使用带有go套件的zap记录器
我想将lib与一起使用,并希望它在此函数中返回实例 对于zap.logger,我将能够像下面那样使用它:(使用zap功能)像使用带有go套件的zap记录器,go,logging,Go,Logging,我想将lib与一起使用,并希望它在此函数中返回实例 对于zap.logger,我将能够像下面那样使用它:(使用zap功能)像 logger.Info 或 logger.WithOptions 等 我尝试用以下方法返回zap接口,但它不起作用,这些方法不可靠,你知道我在做什么吗 我在这里失踪了 func NewZapLogger() zap.Logger { cfg := zap.Config{ Encoding: "json", Level:
logger.Info
或
logger.WithOptions
等
我尝试用以下方法返回zap接口,但它不起作用,这些方法不可靠,你知道我在做什么吗
我在这里失踪了
func NewZapLogger() zap.Logger {
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.FullCallerEncoder,
},
}
logger, _ := cfg.Build()
sugarLogger := logz.NewZapSugarLogger(logger, zap.InfoLevel)
return sugarLogger.
}
Go Kit导出自己的日志记录接口。它们只提供
Log
方法。它被命名为zapSugarLogger
,它基本上是一种与zap
的日志函数(Infow
,Warnw
等)匹配的函数类型
似乎无法从zapSugarLogger
实例访问基础zap功能
但是,您可以自己创建zap
的实例,并像往常一样使用它
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.FullCallerEncoder,
},
}
logger, _ := cfg.Build()
logger.Info("Hello")
}
谢谢,我看到了这个帖子,但我无法使它与zap一起工作,这个例子将非常感谢如果您发布完整的源代码,我将很高兴地修改它并用它更新我的答案。@J我签出了zap代码以及它在go kit中的用法。不幸的是,它的实现方式与logrus不同。我已经更新了我的答案。