logrus 日志示例

发布时间: 更新时间: 总字数:87 阅读时间:1m 作者: 分享 复制网址

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")
}
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数