diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index 1b17e0c4d..770f07d19 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -1,4 +1,4 @@ -// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -128,9 +128,9 @@ func (s *Server) Start() error { } // Logging path setting check. - if s.config.LogPath != "" { + if s.config.LogPath != "" && s.config.LogPath != s.config.Logger.GetPath() { if err := s.config.Logger.SetPath(s.config.LogPath); err != nil { - return gerror.Wrapf(err, `set logging path "%s" failed`, s.config.LogPath) + return err } } // Default session storage. diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index edd63d4ea..9a8a7aa9b 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -1,4 +1,4 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -171,40 +171,21 @@ type ServerConfig struct { // ================================== // Logging. // ================================== - - // Logger specifies the logger for server. - Logger *glog.Logger - - // LogPath specifies the directory for storing logging files. - LogPath string - - // LogStdout specifies whether printing logging content to stdout. - LogStdout bool - - // ErrorStack specifies whether logging stack information when error. - ErrorStack bool - - // ErrorLogEnabled enables error logging content to files. - ErrorLogEnabled bool - - // ErrorLogPattern specifies the error log file pattern like: error-{Ymd}.log - ErrorLogPattern string - - // AccessLogEnabled enables access logging content to files. - AccessLogEnabled bool - - // AccessLogPattern specifies the error log file pattern like: access-{Ymd}.log - AccessLogPattern string + Logger *glog.Logger // Logger specifies the logger for server. + LogPath string // LogPath specifies the directory for storing logging files. + LogLevel string // LogLevel specifies the logging level for logger. + LogStdout bool // LogStdout specifies whether printing logging content to stdout. + ErrorStack bool // ErrorStack specifies whether logging stack information when error. + ErrorLogEnabled bool // ErrorLogEnabled enables error logging content to files. + ErrorLogPattern string // ErrorLogPattern specifies the error log file pattern like: error-{Ymd}.log + AccessLogEnabled bool // AccessLogEnabled enables access logging content to files. + AccessLogPattern string // AccessLogPattern specifies the error log file pattern like: access-{Ymd}.log // ================================== // PProf. // ================================== - - // PProfEnabled enables PProf feature. - PProfEnabled bool - - // PProfPattern specifies the PProf service pattern for router. - PProfPattern string + PProfEnabled bool // PProfEnabled enables PProf feature. + PProfPattern string // PProfPattern specifies the PProf service pattern for router. // ================================== // Other. @@ -267,6 +248,7 @@ func NewConfig() ServerConfig { SessionPath: gsession.DefaultStorageFilePath, SessionCookieOutput: true, Logger: glog.New(), + LogLevel: "all", LogStdout: true, ErrorStack: true, ErrorLogEnabled: true, @@ -334,6 +316,14 @@ func (s *Server) SetConfig(c ServerConfig) error { if c.TLSConfig == nil && c.HTTPSCertPath != "" { s.EnableHTTPS(c.HTTPSCertPath, c.HTTPSKeyPath) } + // Logging. + if s.config.LogPath != "" && s.config.LogPath != s.config.Logger.GetPath() { + if err := s.config.Logger.SetPath(s.config.LogPath); err != nil { + return err + } + } + s.config.Logger.SetLevelStr(s.config.LogLevel) + SetGraceful(c.Graceful) intlog.Printf("SetConfig: %+v", s.config) return nil diff --git a/net/ghttp/ghttp_server_config_logging.go b/net/ghttp/ghttp_server_config_logging.go index 8643b56d4..3ea28f0de 100644 --- a/net/ghttp/ghttp_server_config_logging.go +++ b/net/ghttp/ghttp_server_config_logging.go @@ -1,4 +1,4 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -6,18 +6,26 @@ package ghttp -import "github.com/gogf/gf/internal/intlog" - // SetLogPath sets the log path for server. // It logs content to file only if the log path is set. -func (s *Server) SetLogPath(path string) { +func (s *Server) SetLogPath(path string) error { if len(path) == 0 { - return + return nil } - intlog.Print("SetLogPath:", path) s.config.LogPath = path s.config.ErrorLogEnabled = true s.config.AccessLogEnabled = true + if s.config.LogPath != "" && s.config.LogPath != s.config.Logger.GetPath() { + if err := s.config.Logger.SetPath(s.config.LogPath); err != nil { + return err + } + } + return nil +} + +// SetLogLevel sets logging level by level string. +func (s *Server) SetLogLevel(level string) { + s.config.LogLevel = level } // SetLogStdout sets whether output the logging content to stdout. diff --git a/net/ghttp/ghttp_unit_config_test.go b/net/ghttp/ghttp_unit_config_test.go index 45a049cfd..29cdc4f00 100644 --- a/net/ghttp/ghttp_unit_config_test.go +++ b/net/ghttp/ghttp_unit_config_test.go @@ -52,7 +52,7 @@ func Test_SetConfigWithMap(t *testing.T) { "AccessLogEnabled": true, "ErrorLogEnabled": true, "PProfEnabled": true, - "LogPath": "/var/log/MyServerLog", + "LogPath": "/tmp/log/MyServerLog", "SessionIdName": "MySessionId", "SessionPath": "/tmp/MySessionStoragePath", "SessionMaxAge": 24 * time.Hour, diff --git a/os/glog/glog_logger_config.go b/os/glog/glog_logger_config.go index 924a9e2e4..b51b75986 100644 --- a/os/glog/glog_logger_config.go +++ b/os/glog/glog_logger_config.go @@ -192,7 +192,7 @@ func (l *Logger) SetPath(path string) error { if !gfile.Exists(path) { if err := gfile.Mkdir(path); err != nil { //fmt.Fprintln(os.Stderr, fmt.Sprintf(`[glog] mkdir "%s" failed: %s`, path, err.Error())) - return gerror.Wrapf(err, `Mkdir "%s" failed in Pwd "%s"`, path, gfile.Pwd()) + return gerror.Wrapf(err, `Mkdir "%s" failed in PWD "%s"`, path, gfile.Pwd()) } } l.config.Path = strings.TrimRight(path, gfile.Separator) diff --git a/os/gsession/gsession_storage_file.go b/os/gsession/gsession_storage_file.go index a256b0eec..b97dd820a 100644 --- a/os/gsession/gsession_storage_file.go +++ b/os/gsession/gsession_storage_file.go @@ -56,7 +56,7 @@ func NewStorageFile(path ...string) *StorageFile { } if storagePath != "" { if err := gfile.Mkdir(storagePath); err != nil { - panic(gerror.Wrapf(err, `Mkdir "%s" failed in Pwd "%s"`, path, gfile.Pwd())) + panic(gerror.Wrapf(err, `Mkdir "%s" failed in PWD "%s"`, path, gfile.Pwd())) } } s := &StorageFile{