background
In order to facilitate viewing of logs, the project needs to save the logs to the corresponding log files, so the current configuration is required so that the logs can be saved to the corresponding log files.
Code
import ( "/orandin/lumberjackrus" "/sirupsen/logrus" ) func init() { (NewRotateHook()) } func NewRotateHook() { hook, _ := ( &{ // General log configuration Filename: "", MaxSize: 100, MaxBackups: 1, MaxAge: 1, Compress: false, LocalTime: false, }, , &{DisableColors: true}, &{ // Configuration for different log levels : &{ Filename: "", MaxSize: 100, MaxBackups: 1, MaxAge: 1, Compress: false, LocalTime: false, }, : &{ Filename: "", MaxSize: 100, MaxBackups: 1, MaxAge: 1, Compress: false, LocalTime: false, }, : &{ Filename: "", MaxSize: 100, MaxBackups: 1, MaxAge: 1, Compress: false, LocalTime: false, }, : &{ Filename: "", MaxSize: 10, MaxBackups: 10, MaxAge: 10, Compress: false, LocalTime: false, }, : &{ Filename: "", MaxSize: 10, MaxBackups: 10, MaxAge: 10, Compress: false, LocalTime: false, }, }, ) return hook }
Attachment: logrus features
logrus has the following characteristics:
- Fully compatible with golang standard library log module. logrus has six log levels: debug, info, warning, error, fatal, and panic, which are supersets of the API of the golang standard library log module. If your project uses the standard library log module, you can migrate to logrus at the lowest cost.
- Extensible Hook mechanism. Users are allowed to distribute logs to any place through hooks, such as local file system, standard output, logstash, elasticsearch or mq, etc., or define log content and format through hooks.
- Optional log output format.logrus has two log formats built in, JSONFormatter and TextFormatter.If these two formats do not meet the needs, you can implement the interface Formatter by yourself to define your own log format.
- Field mechanism. logrus encourages refined and structured logging through the Field mechanism rather than logging through lengthy messages.
- logrus is a pluggable, structured logging framework.
Summarize
This is the article about how Golang customizes logs to save as log files. For more information about Golang custom logs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!