Home >Backend Development >Golang >How to design golang log
Golang’s native log module cannot meet the needs, and open source third-party packages are not completely sufficient. It is well known that logrus, which has many users, does not have a rotate function. For operation and maintenance, of course, we hope that the processing of logs will be simple, practical, and sufficient. No additional pass-through system is required to implement logrotate.
1. Demand
In terms of demand, there are several main aspects: (Recommended learning: go )
On the one hand, it is definitely necessary to have a rotation function, and to limit the number of retained log copies. As for whether to cut by file size, day or hour, we can discuss it.
On the other hand, if the log level can be dynamically adjusted, it will be convenient for the log to close the regular log. During online debugging, the log level can be dynamically lowered and more debug logs can be printed.
2. Research
There are several main ones at present:
github.com/sirupsen/logrus。用户广泛,但明确表示不支持日志切割功能,建议通过hook走日志系统。 github.com/natefinch/lumberjack。支持以文件大小的方式切割日志。用户偏少。 gopkg.in/inconshreveable/log15.v2。比较老牌。 github.com/lestrrat-go/file-rotatelogs。基于小时数进行切割的小众包。 github.com/xiaomi-tc/log15。小米基于log15写的二次封装增加了切割功能。但只有2星。
A former colleague wrote based on log15 Secondary encapsulation, but the log15.v2/ext package is not easy to understand.
github.com/ngaut/log。完全不依赖第三方包。可基于日期及小时进行切割。
To sum up, ngaut is more suitable for learning and understanding the encapsulation process. Based on logrus, it is a more reliable choice. The bottom layer requires the cutting function implemented by other packages.
3. Functional design
Main points:
The logging function is completed by logrus.
Log cutting needs to be completed, and historical logs need to be compressed
Support level hot updates provided by referring to problems encountered in Go code
The above is the detailed content of How to design golang log. For more information, please follow other related articles on the PHP Chinese website!