🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## logrus-OverView ***** 一个用于记录日志的库,用于大型系统可以进行日志切割,过期清理,兼容golango原本对日志库 ``` log "github.com/sirupsen/logrus" ``` ## logrus-Usage ***** ### 基本使用 ``` package main import ( log "github.com/sirupsen/logrus" ) func main() { log.WithFields(log.Fields{ "animal": "walrus", }).Info("A walrus appears") } ``` `logrus`与golang标准库日志模块完全兼容,可以使用`log“github.com/sirupsen/logrus”`替换所有日志导入.`logrus`可以通过简单的配置,来定义输出、格式或者日志级别等. ` ` ### 日志切割,过期清理 结合 `github.com/lestrrat-go/file-rotatelogs`包可以对日志进行 日志切割,过期清理: ``` package logger import ( rotations "github.com/lestrrat-go/file-rotatelogs" "github.com/rifflock/lfshook" log "github.com/sirupsen/logrus" "path" "time" ) var Logger = log.New() func init() { baseLogPath := path.Join("../../runtime", "test") /* 日志轮转相关函数 `WithLinkName` 为最新的日志建立软连接 `WithRotationTime` 设置日志分割的时间,隔多久分割一次 WithMaxAge 和 WithRotationCount二者只能设置一个 `WithMaxAge` 设置文件清理前的最长保存时间 `WithRotationCount` 设置文件清理前最多保存的个数 */ // 下面配置日志每隔 1 分钟轮转一个新文件,保留最近 3 分钟的日志文件,多余的自动清理掉。 writer, _ := rotations.New( baseLogPath+".%Y%m%d%H%M", rotations.WithLinkName(baseLogPath), rotations.WithMaxAge(time.Duration(180)*time.Second), rotations.WithRotationTime(time.Duration(60)*time.Second), ) lfHook := lfshook.NewHook(lfshook.WriterMap{ log.DebugLevel: writer, // 为不同级别设置不同的输出目的 log.InfoLevel: writer, log.WarnLevel: writer, log.ErrorLevel: writer, log.FatalLevel: writer, log.PanicLevel: writer, },&log.TextFormatter{DisableColors: true}) Logger.AddHook(lfHook) Logger.WithFields(log.Fields{ "animal": "walrus", "size": 10, }).Info("A group of walrus emerges from the ocean") } ``` ## Zap 参考链接:[https://www.liwenzhou.com/posts/Go/zap/](https://www.liwenzhou.com/posts/Go/zap/) 基本使用+日志切割 ``` package main import ( "net/http" "github.com/natefinch/lumberjack" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var sugarLogger *zap.SugaredLogger func main() { InitLogger() defer sugarLogger.Sync() simpleHttpGet("www.sogo.com") simpleHttpGet("http://www.sogo.com") } func InitLogger() { writeSyncer := getLogWriter() encoder := getEncoder() core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel) logger := zap.New(core, zap.AddCaller()) sugarLogger = logger.Sugar() } func getEncoder() zapcore.Encoder { encoderConfig := zap.NewProductionEncoderConfig() encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder return zapcore.NewConsoleEncoder(encoderConfig) } func getLogWriter() zapcore.WriteSyncer { lumberJackLogger := &lumberjack.Logger{ Filename: "./test.log", MaxSize: 1, MaxBackups: 5, MaxAge: 30, Compress: false, } fileWriteSyncer := zapcore.AddSync(lumberJackLogger) return fileWriteSyncer //return zapcore.NewMultiWriteSyncer(fileWriteSyncer,zapcore.AddSync(os.Stdout)) //这一步是将日志输出到控制台 } func simpleHttpGet(url string) { sugarLogger.Debugf("Trying to hit GET request for %s", url) resp, err := http.Get(url) if err != nil { sugarLogger.Errorf("Error fetching URL %s : Error = %s", url, err) } else { sugarLogger.Infof("Success! statusCode = %s for URL %s", resp.Status, url) resp.Body.Close() } } ```