Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions net/ghttp/ghttp_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_server_config_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_server_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Loading