1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

fix(net/ghttp): nil pointer panic error when server logger set nil (#4055)

This commit is contained in:
John Guo 2024-12-19 22:31:17 +08:00 committed by GitHub
parent 4c2a78b7bf
commit 594979c5af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 6 deletions

View File

@ -343,6 +343,9 @@ func (s *Server) SetConfigWithMap(m map[string]interface{}) error {
if k, v := gutil.MapPossibleItemByKey(m, "FormParsingMemory"); k != "" {
m[k] = gfile.StrToSize(gconv.String(v))
}
if _, v := gutil.MapPossibleItemByKey(m, "Logger"); v == nil {
intlog.Printf(context.TODO(), "SetConfigWithMap: set Logger nil")
}
// Update the current configuration object.
// It only updates the configured keys not all the object.
if err := gconv.Struct(m, &s.config); err != nil {
@ -379,8 +382,10 @@ func (s *Server) SetConfig(c ServerConfig) error {
return err
}
}
if err := s.config.Logger.SetLevelStr(s.config.LogLevel); err != nil {
intlog.Errorf(context.TODO(), `%+v`, err)
if s.config.Logger != nil {
if err := s.config.Logger.SetLevelStr(s.config.LogLevel); err != nil {
intlog.Errorf(context.TODO(), `%+v`, err)
}
}
gracefulEnabled = c.Graceful
intlog.Printf(context.TODO(), "SetConfig: %+v", s.config)

View File

@ -68,10 +68,10 @@ func (s *Server) GetLogPath() string {
// IsAccessLogEnabled checks whether the access log enabled.
func (s *Server) IsAccessLogEnabled() bool {
return s.config.AccessLogEnabled
return s.config.AccessLogEnabled && s.config.Logger != nil
}
// IsErrorLogEnabled checks whether the error log enabled.
func (s *Server) IsErrorLogEnabled() bool {
return s.config.ErrorLogEnabled
return s.config.ErrorLogEnabled && s.config.Logger != nil
}

View File

@ -31,7 +31,7 @@ func (s *Server) handleAccessLog(r *Request) {
r.GetClientIp(), r.Referer(), r.UserAgent(),
)
logger := instance.GetOrSetFuncLock(loggerInstanceKey, func() interface{} {
l := s.Logger().Clone()
l := s.Logger()
l.SetFile(s.config.AccessLogPattern)
l.SetStdoutPrint(s.config.LogStdout)
l.SetLevelPrint(false)
@ -73,7 +73,7 @@ func (s *Server) handleErrorLog(err error, r *Request) {
content += ", " + err.Error()
}
logger := instance.GetOrSetFuncLock(loggerInstanceKey, func() interface{} {
l := s.Logger().Clone()
l := s.Logger()
l.SetStack(false)
l.SetFile(s.config.ErrorLogPattern)
l.SetStdoutPrint(s.config.LogStdout)

View File

@ -618,3 +618,15 @@ func Test_Issue3789(t *testing.T) {
t.Assert(c.GetContent(ctx, "/hello?id=&secondId=2&thirdId=3"), expect)
})
}
// https://github.com/gogf/gf/issues/4047
func Test_Issue4047(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
err := s.SetConfigWithMap(g.Map{
"logger": nil,
})
t.AssertNil(err)
t.Assert(s.Logger(), nil)
})
}