logx
#
Example#
Initializationlogx has many configurable items, you can refer to the definition in logx.LogConf. Currently available
Perform the initial configuration. If the initial configuration is not performed, all the configurations will use the default configuration.
#
LevelThe print log levels supported by logx are:
- info
- error
- server
- fatal
- slow
- stat
You can use the corresponding method to print out the log of the corresponding level. At the same time, in order to facilitate debugging and online use, the log printing level can be dynamically adjusted. The level can be set through logx.SetLevel(uint32) or through configuration initialization. The currently supported parameters are:
#
Log modeAt present, the log printing mode is mainly divided into two types, one is file output, and the other is console output. The recommended way, when using k8s, docker and other deployment methods, you can output the log to the console, use the log collector to collect and import it to es for log analysis. If it is a direct deployment method, the file output method can be used, and logx will automatically create log files corresponding to 5 corresponding levels in the specified file directory to save the logs.
At the same time, the file will be divided according to the natural day. When the specified number of days is exceeded, the log file will be automatically deleted, packaged and other operations.
#
Disable logIf you don't need log printing, you can use logx.Close() to close the log output. Note that when log output is disabled, it cannot be opened again. For details, please refer to the implementation of logx.RotateLogger and logx.DailyRotateRule.
#
Close logBecause logx uses asynchronous log output, if the log is not closed normally, some logs may be lost. The log output must be turned off where the program exits:
Log configuration and shutdown related operations have already been done in most places such as rest and zrpc in the framework, so users don't need to care. At the same time, note that when the log output is turned off, the log cannot be printed again.
Recommended writing:
#
DurationWhen we print the log, we may need to print the time-consuming situation, we can use logx.WithDuration(time.Duration), refer to the following example:
Will output the following format:
In this way, it is easy to collect statistics about slow sql related information.
#
TraceLogtracingEntry is customized for link tracing log output. You can print the traceId and spanId information in the context. With our rest and zrpc, it is easy to complete the related printing of the link log. The example is as follows:
#
SysLogSome applications may use system log for log printing. Logx uses the same encapsulation method, which makes it easy to collect log-related logs into logx.
#
Log configuration relatedLogConf Define the basic configuration required for the logging system
The complete definition is as follows:
#
ModeMode defines the log printing method. The default mode is console, which will print to the console.
The currently supported modes are as follows:
- console
- Print to the console
- file
- Print to access.log, error.log, stat.log and other files in the specified path
- volume
- In order to print to the storage that the mount comes in in k8s, because multiple pods may overwrite the same file, the volume mode automatically recognizes the pod and writes separate log files according to the pod.
#
PathPath defines the output path of the file log, the default value is logs.
#
LevelLevel defines the log printing level, and the default value is info. The currently supported levels are as follows:
- info
- error
- severe
#
CompressCompress defines whether the log needs to be compressed, the default value is false. When Mode is file mode, the file will finally be packaged and compressed into a .gz file.
#
KeepDaysKeepDays defines the maximum number of days to keep logs. The default value is 0, which means that old logs will not be deleted. When Mode is file mode, if the maximum retention days are exceeded, the old log files will be deleted.
#
StackCooldownMillisStackCooldownMillis defines the log output interval, the default is 100 milliseconds.