Zap log rolling log file

The uber-go/zap library does not native support Rolling logging, but it still possible to log and rotate log file via lumberjack 

 

https://pkg.go.dev/gopkg.in/natefinch/lumberjack.v2

The example quick and dirty code

// lumberjack.Logger is already safe for concurrent use, so we don't need to
// lock it.
w := zapcore.AddSync(&lumberjack.Logger{
  Filename:   "/var/log/myapp/foo.log",
  MaxSize:    500, // megabytes
  MaxBackups: 3,
  MaxAge:     28, // days
})
core := zapcore.NewCore(
  zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
  w,
  zap.InfoLevel,
)
logger := zap.New(core)

Addition information:

https://github.com/uber-go/zap/blob/master/FAQ.md#does-zap-support-log-rotation

 

分类: 默认 标签: 发布于: 2024-03-04 09:28:59, 点击数: