The GoFrame
framework provides comprehensive Server
log management features, including access log
and error log
. It is recommended to use configuration files for unified configuration management.
Log Configuration
Configuration Object
Please refer to the API documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/net/ghttp#ServerConfig
Configuration Attributes
The log-related configuration attributes are as follows:
Logger *glog.Logger // Logger for server.
LogPath string // Directory for storing logging files.
LogStdout bool // Printing logging content to stdout.
ErrorStack bool // Logging stack information when error.
ErrorLogEnabled bool // Enable error logging files.
ErrorLogPattern string // Error log file pattern like: error-{Ymd}.log
AccessLogEnabled bool // Enable access logging files.
AccessLogPattern string // Error log file pattern like: access-{Ymd}.log
Brief description:
- By default, logs are not output to files and are printed directly to the terminal. By default, the terminal output for the
access
log is disabled, while theerror
log is enabled. - All options can be set using the
Server.Set*
methods, and most options can be retrieved using theServer.Get*
methods. Logger
is a custom log management object, and developers can also pass a complete log management object to ignore other log option configurations.- The
LogPath
attribute is used to set the log directory, and logs will be output to log files only when a log directory is set. ErrorLogPattern
andAccessLogPattern
are used to configure the log file name format, defaulting toerror-{Ymd}.log
andaccess-{Ymd}.log
, such aserror-20191212.log
,access-20191212.log
.- For descriptions of other configuration options, please refer to the comments and API documentation.
Configuration File
It is officially recommended to use configuration files to manage service and log configurations. Here is an example of a log configuration (in yaml
format):
server:
LogPath: "/var/log/gf-demos/server"
LogStdout: false
ErrorStack: true
ErrorLogEnabled: true
ErrorLogPattern: "error.{Ymd}.log"
AccessLogEnabled: true
AccessLogPattern: "access.{Ymd}.log"
When the Server
starts, it will automatically read the server
node configuration from the default configuration file config.yaml
.
Configuration Method
Log configuration can also be done through the Set*
methods of the Server
object. Refer to the Configuration section.
Log Format
The configuration file method is simpler, so it will not be demonstrated here. The following example uses the configuration method to configure the Server
.
Request Log
Request log:
2018-04-20 18:11:57.344 200 "GET http 127.0.0.1:8199 /log/access HTTP/1.1" 0.120, 127.0.0.1, "", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36"
Log format:
Request Time (to the millisecond) HTTP Status Code "Request Method Request Prefix Request Address Request Protocol" Execution Time (seconds) Client IP "Referrer URL", "UserAgent"
Where Request Prefix
can be http
or https
, and Request Protocol
is typically HTTP/1.0
or HTTP/1.1
.
warning
Note that the Execution Time
recorded in the logs is in seconds
, and in most cases, the time you see is almost 0.xxx
seconds, meaning execution times are in milliseconds, less than 1 second.
Error Log
Error log:
2019-12-20 20:10:56.484 [ERRO] 500, "GET http 127.0.0.1:8199 /log/error HTTP/1.1" 0.210, 127.0.0.1, "", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
Stack:
1. OMG
1). main.main.func1
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/net/ghttp/server/log/log_error.go:10
Error information will print the corresponding stack information where the error occurred (excluding internal framework call information) to facilitate error location and developer analysis.
tip
Any panic
errors generated by the Server
will be automatically captured in the error logs. Therefore, for business programs, regardless of whether it’s in the controller, business encapsulation layer, or data model, if an error occurs and you want to immediately exit the business request handling, just panic
.
Custom Log Handling
Developers can customize the handling of Server
request logs in two ways:
- Custom
*glog.Logger
objects can be passed through log configuration options. - Interception can be handled uniformly through middleware, as described in the Middleware - Intro section.
Server
Logs and Business Logs
This is a FAQ
.
It is important to note that the logs mentioned here are the Server
logs, similar to a series of logs from Web Server
services like nginx
, apache
, tomcat
, etc. Only the Server
is allowed to output content, and developers cannot write log content into the Server
log files, and the log types and formats are completely fixed.
The GoFrame
framework also provides a log module, implemented by the glog
log component. Logs printed by developers through the glog
component are considered business logs, and the program’s business code can decide what content to output, where to output it, and what the output format should be like. The commonly used g.Log()
method is used to output business logs, and this method supports automatically reading the logger
configuration items from the configuration file. Please refer to the Logging section for more details.