Go Programming Languages - 框架篇 - 日志 - Logrus
日志作用
- 定位问题 - 这个就不用多说了,平常排查问题用的最多的方法就是看日志
- 监控 - 这个没咋用过,留个空
- 业务分析 - 统计数据吧,比如有多少请求,多少次I/O请求啥的
日志级别(程度由低到高,大体上是这个顺序)
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
Logrus介绍
https://github.com/sirupsen/logrus
根据github介绍,logrus的定义是
Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.
logrus完全兼容标准的log库,还支持文本、JSON 两种日志输出格式
1 | go get github.com/sirupsen/logrus |
指定输出格式
文本输出格式
1 | logger.SetFormatter(&logrus.TextFormatter{ |
JSON输出格式 1
2
3logger.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05.000",
})
这个包的问题:不会进行日志轮询,即定期进行清理,避免log文件不断大下去
有一个包叫rotatelogs,但已经不维护了
rotatelogs介绍
https://github.com/lestrrat-go/file-rotatelogs
根据定义,rotatelogs的作用是
Provide an io.Writer that periodically rotates log files from within the application
也就是说在应用程序内定期轮换日志文件
安装命令
1 | go get github.com/lestrrat-go/file-rotatelogs |
rotatelogs可以指定:
- 日志名格式
- 软链接
- 每隔多少时间生成一份日志
- 最大保存期限
1 | fout, err := rotatelogs.New( |
设置日志输出方式
1 | // 设置为日志文件输出 |
设置日志调用关系
1 | logger.SetReportCaller(true) |
这样输出的日志就会带上method,比如: 1
2{"animal":"penguin","level":"fatal","method":"github.com/sirupsen/arcticcreatures.migrate","msg":"a penguin swims by",
"time":"2014-03-10 19:57:38.562543129 -0400 EDT"}
钩子(Hooks)
简单来说就是打印日志的时候顺带会执行的操作,比如发短信啦,接入一些平台bot发消息啦
logrus支持hooks请参考官网:https://github.com/sirupsen/logrus/wiki/Hooks
自定义钩子
实现hook的两个方法,一个是Levels
,表示适用于哪些等级,另一个是Fire
,表示具体做法
1 | // 实现logurs.Hook接口 |
其中Entry的结构是
1 | type Entry struct { |
对应生成的日志就是中的话,Message
就是msg
,Level
就是level
,实际存储在的是Data
变量中
1 | {"age":18,"file":"/home/guofangcheng/go-framework/io/logrus_test.go:14","func":"github.com/Jerry20000730/go-framework/io.TestLogrus","level":"info","msg":"this is info log","name":"test","time":"2025-03-24 01:03:48.080"} |
示例代码就是做了个简单的处理,实际可以做更为复杂的逻辑,比如加入kafka,对接到提醒bot等。并且因为修改了Data
,所以本质上可以修改他的日志输出内容。
Go Programming Languages - 框架篇 - 日志 - Logrus
https://jerry20000730.github.io/wiki/程序语言/Go/Go_framework_log/