Skip to main content
Version: 2.8.x(Latest)

The log component is one of the core components of the GoFrame framework, supporting convenient configuration management capabilities.

tip

The log configuration uses the unified configuration component of the framework, supporting multiple file formats, as well as configuration centers and interface-based extensions. For more details, please refer to the section: Configuration

The log component supports configuration files. When using g.Log(instance name) to obtain the Logger instance object, it will automatically obtain the corresponding Logger configuration through the default configuration management object. By default, it will read the logger.instance name configuration item. If this configuration item does not exist, it will read the default logger configuration item. Please refer to the configuration object structure definition for configuration items: https://pkg.go.dev/github.com/gogf/gf/v2/os/glog#Config

The complete configuration file configuration items and descriptions are as follows, with configuration item names being case-insensitive:

logger:
path: "/var/log/" # Log file path. Default is empty, indicating closed, only output to terminal
file: "{Y-m-d}.log" # Log file format. Default is "{Y-m-d}.log"
prefix: "" # Prefix for log content output. Default is empty
level: "all" # Log output level
timeFormat: "2006-01-02T15:04:05" # Custom log output time format, configured using Golang's standard time format
ctxKeys: [] # Custom Context variable names, automatically print Context variables to the log. Default is empty
header: true # Whether to print the header information of the log. Default is true
stdout: true # Whether the log is also output to the terminal. Default is true
rotateSize: 0 # Rotate files based on log file size. Default is 0, indicating that the rotation feature is turned off
rotateExpire: 0 # Rotate files based on log file time interval. Default is 0, indicating that the rotation feature is turned off
rotateBackupLimit: 0 # Clean up split files according to the number of split files, effective when the rotation feature is turned on. Default is 0, meaning no backup, delete when split
rotateBackupExpire: 0 # Clean up split files according to the validity period of split files, effective when the rotation feature is turned on. Default is 0, meaning no backup, delete when split
rotateBackupCompress: 0 # Compression ratio of rotated split files (0-9). Default is 0, meaning no compression
rotateCheckInterval: "1h" # Time detection interval for rotation splitting, generally does not need to be set. Default is 1 hour
stdoutColorDisabled: false # Disable color printing on the terminal. Default is enabled
writerColorEnable: false # Whether the log file is colored. Default is false, indicating no color

Among them, the level configuration item is configured using a string and supports the following configurations according to log levels: DEBU < INFO < NOTI < WARN < ERRO < CRIT, and also supports common deployment mode configuration names such as ALL, DEV, PROD. The level configuration item string is not case-sensitive. For a detailed introduction to log levels, please refer to the section Logging - Log Level.

Example 1, Default Configuration Items

logger:
path: "/var/log"
level: "all"
stdout: false

Then you can use g.Log() to automatically obtain and set this configuration when getting the default instance object.

Example 2, Multiple Configuration Items

An example of configuration for multiple Logger:

logger:
path: "/var/log"
level: "all"
stdout: false
logger1:
path: "/var/log/logger1"
level: "dev"
stdout: false
logger2:
path: "/var/log/logger2"
level: "prod"
stdout: true

We can obtain the corresponding configured Logger instance objects by the instance object name:

// Corresponding to logger.logger1 configuration item
l1 := g.Log("logger1")
// Corresponding to logger.logger2 configuration item
l2 := g.Log("logger2")
// Corresponding to default configuration item logger
l3 := g.Log("none")
// Corresponding to default configuration item logger
l4 := g.Log()

Configuration Method (Advanced)

The configuration method is used for developers to perform configuration management themselves when using glog modularly.

List of methods:

Brief explanation:

  1. You can set using SetConfig and SetConfigWithMap.
  2. You can also use the Set* methods of the Logger object for specific configurations.
  3. It should be noted that configuration items should be set before the Logger object executes log output to avoid concurrent safety issues.

We can use the SetConfigWithMap method to set/modify specific configurations of the Logger through Key-Value pairs, while other configurations use the default configuration. The name of the Key is the attribute name in the Config struct, and it is case-insensitive, supporting -/ _/ space symbols between words. For details, please refer to the conversion rules in the section Type Conversion - Struct.

Simple example:

logger := glog.New()
logger.SetConfigWithMap(g.Map{
"path": "/var/log",
"level": "all",
"stdout": false,
"StStatus": 0,
})
logger.Print("test")

Here StStatus indicates whether stack printing is enabled, set to 0 to disable. The key name can also be stStatus, st-status, st_status, St Status, and other configuration attributes are analogous.

Precautions

Common issues: such as why the log component configuration does not take effect for logs printed by HTTP Server, GRPC Server, ORM components.

The GoFrame framework adopts a modular design, and the log component is an independent component of the framework. The configurations introduced in this chapter only take effect for independently using the log component, such as using g.Log() or glog.New(). The log configuration of other components has their own configuration items or log object setting methods to achieve log configuration. Please refer specifically to the corresponding component documentation and API.