logrus 日志示例

发布时间: 更新时间: 总字数:126 阅读时间:1m 作者: IP上海 分享 网址

logrus是使用Golang开发的日志库,支持日志文件的压缩和分隔

示例

package main

import (
	"github.com/sirupsen/logrus"
	lumberjack "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
	// 配置日志分隔
	lc := lumberjack.Logger{
		Filename:   "a.log",
		MaxSize:    1,
		MaxBackups: 5,
		Compress:   true,
	}
	logrus.SetLevel(logrus.DebugLevel)
	logrus.SetReportCaller(true)
	logrus.SetFormatter(&logrus.TextFormatter{})
	logrus.SetOutput(&lc)

	logrus.Infof("hello logrus\n")
	logrus.Debugf("hello debug\n")
	// 带标签的日志
	logrus.WithFields(logrus.Fields{"x": "y"}).Debug("xxx")
}
  • 高级
    • logrus.WithError() 使用 ErrorKey 中定义的值作为关键字,从标准日志记录器中创建一个条目并添加错误信息
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数