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

Logging - Configuration - 图1tip

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:

  1. logger:
  2. path: "/var/log/" # Log file path. Default is empty, indicating closed, only output to terminal
  3. file: "{Y-m-d}.log" # Log file format. Default is "{Y-m-d}.log"
  4. prefix: "" # Prefix for log content output. Default is empty
  5. level: "all" # Log output level
  6. timeFormat: "2006-01-02T15:04:05" # Custom log output time format, configured using Golang's standard time format
  7. ctxKeys: [] # Custom Context variable names, automatically print Context variables to the log. Default is empty
  8. header: true # Whether to print the header information of the log. Default is true
  9. stdout: true # Whether the log is also output to the terminal. Default is true
  10. rotateSize: 0 # Rotate files based on log file size. Default is 0, indicating that the rotation feature is turned off
  11. rotateExpire: 0 # Rotate files based on log file time interval. Default is 0, indicating that the rotation feature is turned off
  12. 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
  13. 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
  14. rotateBackupCompress: 0 # Compression ratio of rotated split files (0-9). Default is 0, meaning no compression
  15. rotateCheckInterval: "1h" # Time detection interval for rotation splitting, generally does not need to be set. Default is 1 hour
  16. stdoutColorDisabled: false # Disable color printing on the terminal. Default is enabled
  17. 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

  1. logger:
  2. path: "/var/log"
  3. level: "all"
  4. 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:

  1. logger:
  2. path: "/var/log"
  3. level: "all"
  4. stdout: false
  5. logger1:
  6. path: "/var/log/logger1"
  7. level: "dev"
  8. stdout: false
  9. logger2:
  10. path: "/var/log/logger2"
  11. level: "prod"
  12. stdout: true

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

  1. // Corresponding to logger.logger1 configuration item
  2. l1 := g.Log("logger1")
  3. // Corresponding to logger.logger2 configuration item
  4. l2 := g.Log("logger2")
  5. // Corresponding to default configuration item logger
  6. l3 := g.Log("none")
  7. // Corresponding to default configuration item logger
  8. 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:

  1. logger := glog.New()
  2. logger.SetConfigWithMap(g.Map{
  3. "path": "/var/log",
  4. "level": "all",
  5. "stdout": false,
  6. "StStatus": 0,
  7. })
  8. 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.