natefinch / lumberjack

lumberjack is a log rolling package for Go
MIT License
4.81k stars 593 forks source link

Implemented SyncWriter interface #183

Open piyongcai opened 1 year ago

piyongcai commented 1 year ago

Implemented SyncWriter interface.

In some scenarios, we need to use the SyncWriter interface, such as integrating lumberjack and go.uber.org/zap.

piyongcai commented 1 year ago

@elithrar Please check and merge!!

elithrar commented 1 year ago

Please don't ping me. I don't have any relationship to this package.

liuzengh commented 8 months ago

@piyongcai you can use zapcore.AddSync to wrap lumberjack.Logger :

// 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)

logger.Info("Hello, world!")
remones commented 2 months ago

@liuzengh but zapcore.AddSync returns a fake SyncWriter, right? It doesn't call file.Sync()