github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_server_config_logging.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import "github.com/gogf/gf/v2/os/glog"
    10  
    11  // SetLogPath sets the log path for server.
    12  // It logs content to file only if the log path is set.
    13  func (s *Server) SetLogPath(path string) error {
    14  	if len(path) == 0 {
    15  		return nil
    16  	}
    17  	s.config.LogPath = path
    18  	s.config.ErrorLogEnabled = true
    19  	s.config.AccessLogEnabled = true
    20  	if s.config.LogPath != "" && s.config.LogPath != s.config.Logger.GetPath() {
    21  		if err := s.config.Logger.SetPath(s.config.LogPath); err != nil {
    22  			return err
    23  		}
    24  	}
    25  	return nil
    26  }
    27  
    28  // SetLogger sets the logger for logging responsibility.
    29  // Note that it cannot be set in runtime as there may be concurrent safety issue.
    30  func (s *Server) SetLogger(logger *glog.Logger) {
    31  	s.config.Logger = logger
    32  }
    33  
    34  // Logger is alias of GetLogger.
    35  func (s *Server) Logger() *glog.Logger {
    36  	return s.config.Logger
    37  }
    38  
    39  // SetLogLevel sets logging level by level string.
    40  func (s *Server) SetLogLevel(level string) {
    41  	s.config.LogLevel = level
    42  }
    43  
    44  // SetLogStdout sets whether output the logging content to stdout.
    45  func (s *Server) SetLogStdout(enabled bool) {
    46  	s.config.LogStdout = enabled
    47  }
    48  
    49  // SetAccessLogEnabled enables/disables the access log.
    50  func (s *Server) SetAccessLogEnabled(enabled bool) {
    51  	s.config.AccessLogEnabled = enabled
    52  }
    53  
    54  // SetErrorLogEnabled enables/disables the error log.
    55  func (s *Server) SetErrorLogEnabled(enabled bool) {
    56  	s.config.ErrorLogEnabled = enabled
    57  }
    58  
    59  // SetErrorStack enables/disables the error stack feature.
    60  func (s *Server) SetErrorStack(enabled bool) {
    61  	s.config.ErrorStack = enabled
    62  }
    63  
    64  // GetLogPath returns the log path.
    65  func (s *Server) GetLogPath() string {
    66  	return s.config.LogPath
    67  }
    68  
    69  // IsAccessLogEnabled checks whether the access log enabled.
    70  func (s *Server) IsAccessLogEnabled() bool {
    71  	return s.config.AccessLogEnabled
    72  }
    73  
    74  // IsErrorLogEnabled checks whether the error log enabled.
    75  func (s *Server) IsErrorLogEnabled() bool {
    76  	return s.config.ErrorLogEnabled
    77  }