[英]How to configure uber-go/zap logger for rolling filesystem log?
How to configure uber-go/zap
logger api to append logs to a specified file-path.如何配置
uber-go/zap
logger api 以将日志附加到指定的文件路径。 Can it be made to work like rolling file-appender (based on file-size or date) without affecting performance?它可以像滚动文件附加器(基于文件大小或日期)一样工作而不影响性能吗?
A hook can be added to the zap logger which writes the entries to lumberjack , a rolling log for Go.可以向 zap 记录器添加一个钩子,它将条目写入lumberjack ,这是 Go 的滚动日志。
A simple usage would look like this:一个简单的用法如下所示:
The rolling log:滚动日志:
// remember to call this at app (or scope) exit:
// logger.Close()
var lumlog = &lumberjack.Logger{
Filename: "/tmp/my-zap.log",
MaxSize: 10, // megabytes
MaxBackups: 3, // number of log files
MaxAge: 3, // days
}
The zap
compatible hook: zap
兼容钩子:
func lumberjackZapHook(e zapcore.Entry) error {
lumlog.Write([]byte(fmt.Sprintf("%+v", e)))
return nil
}
And use it like:并像这样使用它:
logger, _ := zap.NewProduction(zap.Hooks(lumberjackZapHook))
Edit 1: I'm not sure if this meets your requirement in terms of performance.编辑 1:我不确定这是否符合您在性能方面的要求。 There are many factors there.
这里面有很多因素。 For example using SSD hards make a big difference, or even logging into some timeseries databases with batch writes.
例如,使用 SSD 硬盘会产生很大的不同,甚至可以通过批量写入登录一些时间序列数据库。
Edit 2: In zap documentation too, it uses lumberjack (but not as a hook).编辑 2:在 zap 文档中,它也使用伐木工人(但不是作为挂钩)。
If you want to use both console log and rolling log without hook then you can do the following:如果您想同时使用控制台日志和滚动日志而不使用钩子,那么您可以执行以下操作:
// NewProductionZapLogger will return a new production logger backed by zap
func NewProductionZaplogger() (Logger, error) {
conf := zap.NewProductionConfig()
conf.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
conf.DisableCaller = true
conf.DisableStacktrace = true
zapLogger, err := conf.Build(zap.WrapCore(zapCore))
return zpLg{
lg: zapLogger.Sugar(),
}, err
}
func zapCore(c zapcore.Core) zapcore.Core {
// lumberjack.Logger is already safe for concurrent use, so we don't need to
// lock it.
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "./chat.log",
MaxSize: 50, // megabytes
MaxBackups: 30,
MaxAge: 28, // days
})
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
w,
zap.DebugLevel,
)
cores := zapcore.NewTee(c, core)
return cores
}
I wrote a simpler library rzap for zap and lumberjack.我为 zap 和伐木工人编写了一个更简单的库rzap 。
rzap.NewGlobalLogger([]zapcore.Core{
rzap.NewCore(&lumberjack.Logger{
Filename: "/your/log/path/app.log",
MaxSize: 10, // 10 megabytes, defaults to 100 megabytes
MaxAge: 10, // 10 days, default is not to remove old log files
MaxBackups: 10, // 10 files, default is to retain all old log files
Compress: true, // compress to gzio, default is not to perform compression
}, zap.InfoLevel),
})
zap.L().Info("some message", zap.Int("status", 0))
write just this simple library for lumberjack and zap只为伐木工人和 zap 编写这个简单的库
rzap.NewGlobalLogger([]zapcore.Core{
rzap.NewCore(&lumberjack.Logger{
Filename: "/your/log/path/app.log",
MaxSize: 10, // 10 megabytes, defaults to 100 megabytes
MaxAge: 10, // 10 days, default is not to remove old log files
MaxBackups: 10, // 10 files, default is to retain all old log files
Compress: true, // compress to gzio, default is not to perform compression
}, zap.InfoLevel),
})
zap.L().Info("some message", zap.Int("status", 0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.