Sirupsen/logrus是一个日志库
文档
- https://github.com/Sirupsen/logrus
安装
go get github.com/sirupsen/logrus
代码示例
package main
import (
"github.com/sirupsen/logrus"
)
func main() {
var log = logrus.New()
log.Trace("Something very low level.")
log.Debug("Useful debugging information.")
log.Info("Something noteworthy happened!")
log.Warn("You should probably take a look at this.")
log.Error("Something failed but I'm not quitting.")
}
输出
time="2024-05-27T11:27:24+08:00" level=info msg="Something noteworthy happened!"
time="2024-05-27T11:27:24+08:00" level=warning msg="You should probably take a look at this."
time="2024-05-27T11:27:24+08:00" level=error msg="Something failed but I'm not quitting."
其他参数
// 1、设置格式
// json格式:&logrus.JSONFormatter{}
// 文本格式(默认):&log.TextFormatter{}
log.SetFormatter(&logrus.JSONFormatter{})
// 2、设置输出到文件
// 2.1、标准输出
log.Out = os.Stdout
// 2.2、文件输出
file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
defer file.Close()
if err == nil {
log.Out = file
} else {
log.Info("Failed to log to file, using default stderr")
}
// 3、设置日志级别
log.SetLevel(logrus.DebugLevel)