mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add logging level configuration for package ghttp
This commit is contained in:
@ -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.
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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{
|
||||
|
||||
Reference in New Issue
Block a user