gf/net/ghttp/ghttp_server_config_logging.go

78 lines
2.1 KiB
Go
Raw Normal View History

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2018-09-14 13:02:13 +08:00
//
// 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,
// You can obtain one at https://github.com/gogf/gf.
2018-09-14 13:02:13 +08:00
package ghttp
2021-10-11 21:41:56 +08:00
import "github.com/gogf/gf/v2/os/glog"
2020-05-01 03:31:04 +08:00
// 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) error {
2019-06-19 09:06:52 +08:00
if len(path) == 0 {
return nil
2019-06-19 09:06:52 +08:00
}
s.config.LogPath = path
2019-09-26 20:01:48 +08:00
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
}
// SetLogger sets the logger for logging responsibility.
// Note that it cannot be set in runtime as there may be concurrent safety issue.
func (s *Server) SetLogger(logger *glog.Logger) {
s.config.Logger = logger
}
// Logger is alias of GetLogger.
func (s *Server) Logger() *glog.Logger {
return s.config.Logger
}
// SetLogLevel sets logging level by level string.
func (s *Server) SetLogLevel(level string) {
s.config.LogLevel = level
2018-09-14 13:02:13 +08:00
}
2020-05-01 03:31:04 +08:00
// SetLogStdout sets whether output the logging content to stdout.
2019-06-19 09:06:52 +08:00
func (s *Server) SetLogStdout(enabled bool) {
s.config.LogStdout = enabled
}
2020-05-01 03:31:04 +08:00
// SetAccessLogEnabled enables/disables the access log.
2019-06-19 09:06:52 +08:00
func (s *Server) SetAccessLogEnabled(enabled bool) {
s.config.AccessLogEnabled = enabled
2018-09-14 13:02:13 +08:00
}
2020-05-01 03:31:04 +08:00
// SetErrorLogEnabled enables/disables the error log.
2019-06-19 09:06:52 +08:00
func (s *Server) SetErrorLogEnabled(enabled bool) {
s.config.ErrorLogEnabled = enabled
2018-09-14 13:02:13 +08:00
}
2020-05-01 03:31:04 +08:00
// SetErrorStack enables/disables the error stack feature.
func (s *Server) SetErrorStack(enabled bool) {
s.config.ErrorStack = enabled
}
2020-05-01 03:31:04 +08:00
// GetLogPath returns the log path.
2019-06-19 09:06:52 +08:00
func (s *Server) GetLogPath() string {
return s.config.LogPath
2018-09-14 13:02:13 +08:00
}
2020-05-01 03:31:04 +08:00
// IsAccessLogEnabled checks whether the access log enabled.
2019-06-19 09:06:52 +08:00
func (s *Server) IsAccessLogEnabled() bool {
return s.config.AccessLogEnabled
2018-09-14 13:02:13 +08:00
}
2020-05-01 03:31:04 +08:00
// IsErrorLogEnabled checks whether the error log enabled.
2019-06-19 09:06:52 +08:00
func (s *Server) IsErrorLogEnabled() bool {
return s.config.ErrorLogEnabled
2018-09-14 13:02:13 +08:00
}