使用Lumberjack进行日志切割归档
Zap本身不支持切割归档日志文件
为了添加日志切割归档功能,我们将使用第三方库Lumberjack来实现
go get -u github.com/natefinch/lumberjack
要在zap中加入Lumberjack支持,我们需要修改WriteSyncer代码。我们将按照下面的代码修改getLogWriter()函数
func getLogWriter() zapcore.WriteSyncer { lumberJackLogger := &lumberjack.Logger{ Filename: "./test.log", //日志文件的位置 MaxSize: 1, //在进行切割之前,日志文件的最大大小(以MB为单位) MaxBackups: 5, //保留旧文件的最大个数 MaxAge: 30, //保留旧文件的最大天数 Compress: false, //是否压缩/归档旧文件 } return zapcore.AddSync(lumberJackLogger) }
测试
package main import ( "github.com/natefinch/lumberjack" "go.uber.org/zap" "go.uber.org/zap/zapcore" "net/http" "sync" ) var sugarLogger *zap.SugaredLogger var wg sync.WaitGroup func main() { InitLogger() defer sugarLogger.Sync() for i := 0; i < 1500; i++ { wg.Add(2) go simpleHttpGet("www.baidu.com") go simpleHttpGet("https://www.baidu.com") } wg.Wait() } func InitLogger() { writeSyncer := getLogWriter() encoder := getEncoder() core := zapcore.NewCore(encoder, writeSyncer, zapcore.DebugLevel) logger := zap.New(core) sugarLogger = logger.Sugar() } func getEncoder() zapcore.Encoder { return zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()) } func getLogWriter() zapcore.WriteSyncer { lumberJackLogger := &lumberjack.Logger{ Filename: "./test.log", //日志文件的位置 MaxSize: 1, //在进行切割之前,日志文件的最大大小(以MB为单位) MaxBackups: 5, //保留旧文件的最大个数 MaxAge: 30, //保留旧文件的最大天数 Compress: false, //是否压缩/归档旧文件 } return zapcore.AddSync(lumberJackLogger) } 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() } wg.Done() }