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:

  1. Logger *glog.Logger // Logger for server.
  2. LogPath string // Directory for storing logging files.
  3. LogStdout bool // Printing logging content to stdout.
  4. ErrorStack bool // Logging stack information when error.
  5. ErrorLogEnabled bool // Enable error logging files.
  6. ErrorLogPattern string // Error log file pattern like: error-{Ymd}.log
  7. AccessLogEnabled bool // Enable access logging files.
  8. AccessLogPattern string // Error log file pattern like: access-{Ymd}.log

Brief description:

  1. 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 the error log is enabled.
  2. All options can be set using the Server.Set* methods, and most options can be retrieved using the Server.Get* methods.
  3. Logger is a custom log management object, and developers can also pass a complete log management object to ignore other log option configurations.
  4. 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.
  5. ErrorLogPattern and AccessLogPattern are used to configure the log file name format, defaulting to error-{Ymd}.log and access-{Ymd}.log, such as error-20191212.log, access-20191212.log.
  6. 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):

  1. server:
  2. LogPath: "/var/log/gf-demos/server"
  3. LogStdout: false
  4. ErrorStack: true
  5. ErrorLogEnabled: true
  6. ErrorLogPattern: "error.{Ymd}.log"
  7. AccessLogEnabled: true
  8. 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:

  1. 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:

  1. 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.

Logging - 图1warning

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:

  1. 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"
  2. Stack:
  3. 1. OMG
  4. 1). main.main.func1
  5. /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.

Logging - 图2tip

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:

  1. Custom *glog.Logger objects can be passed through log configuration options.
  2. 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.