This is a creation in Article, where the information may have evolved or changed.
Golang Log module designed for your own use
Golang's native log module does not meet demand, and open source third-party packages are not fully sufficient. Users are more logrus, but there is no rotate function, this is already known. For operation and maintenance, of course, it is hoped that the log processing is relatively simple, practical, enough. There is no need for an additional system to implement Logrotate.
1. Demand
In terms of demand, there are several main aspects:
On the one hand, it is necessary to have a rotation function, and to limit the number of log copies reserved. As to whether it is cut by file size or by day or by the hour, it can be discussed.
On the other hand, for the level of the log, if you can dynamically adjust, it is convenient to log off the regular log, online debugging, dynamically reduce the level of log, print more debug logs.
2. Research
At present, there are several main:
- Github.com/sirupsen/logrus. A wide range of users, but it is clear that the log cutting function is not supported, it is recommended to go through the log system hook.
- Github.com/natefinch/lumberjack. Supports cutting logs in a file size manner. Fewer users.
- Gopkg.in/inconshreveable/log15.v2. More veteran.
- Github.com/lestrrat-go/file-rotatelogs. A small crowdsourcing package that is cut based on the number of hours.
- Github.com/xiaomi-tc/log15. Xiaomi's two-time package based on LOG15 adds cutting functionality. But only 2 stars.
- There are two packages written by former colleagues based on LOG15, but Log15.v2/ext's package is not easy to understand.
- Github.com/ngaut/log. Do not rely on third-party packages at all. Can be cut based on date and hour.
In conclusion, Ngaut is more suitable for learning and understanding the encapsulation process. And based on Logrus is a more reliable choice, the bottom of the need for other packages to achieve the cutting function.
3. Function design
Main points:
- The log function is completed by Logrus.
- Need to complete log cut, history log needs to be compressed
- Supports the level hot update provided by reference to issues encountered with go code
3.1 Based on file-rotatelogs implementation
That is, a date-based log rotation.
Code: Github.com/jungle85gopy/rotlogs/daterot
Example code: Reference Github.com/jungle85gopy/rotlogs/example/date/date.go
Two deficiencies:
- The library does not have compression capabilities.
- The log directory is unfriendly to the relative path.
- For example, when using Log/access.log. Log/access.log is a soft chain that links to files with logs.
# ls -l loglrwxr-xr-x 1 song staff 28 Mar 8 13:23 access.log -> log/access.log.20180308-1323-rw-r--r-- 1 song staff 2511 Mar 8 13:23 access.log.20180308-1323# tail log/access.logtail: log/access.log: No such file or directory
One solution is to not use the soft chain, which will be to not set the BASELINKNAME variable actively.
3.2 Based on lumberjack implementation
This is the file size based log rotation.
Code: Github.com/jungle85gopy/rotlogs
Example code: Reference Github.com/jungle85gopy/rotlogs/example/size/size.go
The disadvantage of this version is that the log is cut Dogo Katsuragi and its file name is too complex. Possible inconvenience after subsequent treatment
# ls -l log abcd-2018-03-08T14-09-31.066.log.gzabcd-2018-03-08T14-26-18.794.log.gz