Uber Zap 记录器不在日志语句中打印呼叫者信息
Uber Zap logger not printing caller information in the log statement
我正在尝试使用配置的自定义编码器将相同的消息同时发送到控制台和日志文件。在此过程中,我想显示来电者信息,但即使我按照文档中的建议使用了 caller
键,也不会显示相同的信息。
下面是相同的示例代码
package main
import (
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var logLevelSeverity = map[zapcore.Level]string{
zapcore.DebugLevel: "DEBUG",
zapcore.InfoLevel: "INFO",
zapcore.WarnLevel: "WARNING",
zapcore.ErrorLevel: "ERROR",
zapcore.DPanicLevel: "CRITICAL",
zapcore.PanicLevel: "ALERT",
zapcore.FatalLevel: "EMERGENCY",
}
func SyslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("Jan 01, 2006 15:04:05"))
}
func CustomEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(logLevelSeverity[level])
}
func CustomLevelFileEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + logLevelSeverity[level] + "]")
}
func main() {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "temp1.log",
MaxSize: 1024,
MaxBackups: 20,
MaxAge: 28,
Compress: true,
})
//Define config for the console output
cfgConsole := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "severity",
EncodeLevel: CustomEncodeLevel,
TimeKey: "time",
EncodeTime: SyslogTimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.FullCallerEncoder,
}
cfgFile := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "severity",
EncodeLevel: CustomLevelFileEncoder,
TimeKey: "time",
EncodeTime: SyslogTimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.FullCallerEncoder,
}
consoleDebugging := zapcore.Lock(os.Stdout)
//consoleError := zapcore.Lock(os.Stderr)
core := zapcore.NewTee(
zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel),
zapcore.NewCore(zapcore.NewJSONEncoder(cfgConsole), consoleDebugging, zap.DebugLevel),
//zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), consoleError, zap.ErrorLevel),
)
//core := zapcore.NewCore(zapcore.NewConsoleEncoder(encConsole), w, zap.DebugLevel)
wlogger := zap.New(core)
wlogger.Debug("Sample debug for log file and console")
wlogger.Warn("An warning message example")
wlogger.Info("An info level message")
coreFile := zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel)
flogger := zap.New(coreFile)
flogger.Debug("An exclusive message for file")
//output
//{"severity":"DEBUG","time":"Nov 11, 2018 20:24:11","message":"Sample debug for log file and console"}
//{"severity":"WARNING","time":"Nov 11, 2018 20:24:11","message":"An warning message example"}
//{"severity":"INFO","time":"Nov 11, 2018 20:24:11","message":"An info level message"}
}
有没有想过为什么没有显示来电信息?
根据文档https://godoc.org/go.uber.org/zap#AddCaller
你可以在创建记录器时做这样的事情:
wlogger := zap.New(core, zap.AddCaller())
更新评论回复
您还可以定义调用者编码的实现:
func MyCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(filepath.Base(caller.FullPath()))
}
并将其传递给 cfgConsole 和 cfgFile
AddCallerSkip 可以帮助您跳过调用者的调用堆栈深度。
// before
zap.New(core, zap.AddCaller())
// output: {"level":"panic","time":"2020-06-09T16:08:25.677+0800","line":"log/log.go:118","msg":"before")}
// add caller skip
zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
// output: {"level":"panic","time":"2020-06-09T16:47:39.559+0800","line":"client/client.go:29","msg":"after"}
我正在尝试使用配置的自定义编码器将相同的消息同时发送到控制台和日志文件。在此过程中,我想显示来电者信息,但即使我按照文档中的建议使用了 caller
键,也不会显示相同的信息。
下面是相同的示例代码
package main
import (
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var logLevelSeverity = map[zapcore.Level]string{
zapcore.DebugLevel: "DEBUG",
zapcore.InfoLevel: "INFO",
zapcore.WarnLevel: "WARNING",
zapcore.ErrorLevel: "ERROR",
zapcore.DPanicLevel: "CRITICAL",
zapcore.PanicLevel: "ALERT",
zapcore.FatalLevel: "EMERGENCY",
}
func SyslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("Jan 01, 2006 15:04:05"))
}
func CustomEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(logLevelSeverity[level])
}
func CustomLevelFileEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + logLevelSeverity[level] + "]")
}
func main() {
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "temp1.log",
MaxSize: 1024,
MaxBackups: 20,
MaxAge: 28,
Compress: true,
})
//Define config for the console output
cfgConsole := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "severity",
EncodeLevel: CustomEncodeLevel,
TimeKey: "time",
EncodeTime: SyslogTimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.FullCallerEncoder,
}
cfgFile := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "severity",
EncodeLevel: CustomLevelFileEncoder,
TimeKey: "time",
EncodeTime: SyslogTimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.FullCallerEncoder,
}
consoleDebugging := zapcore.Lock(os.Stdout)
//consoleError := zapcore.Lock(os.Stderr)
core := zapcore.NewTee(
zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel),
zapcore.NewCore(zapcore.NewJSONEncoder(cfgConsole), consoleDebugging, zap.DebugLevel),
//zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), consoleError, zap.ErrorLevel),
)
//core := zapcore.NewCore(zapcore.NewConsoleEncoder(encConsole), w, zap.DebugLevel)
wlogger := zap.New(core)
wlogger.Debug("Sample debug for log file and console")
wlogger.Warn("An warning message example")
wlogger.Info("An info level message")
coreFile := zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel)
flogger := zap.New(coreFile)
flogger.Debug("An exclusive message for file")
//output
//{"severity":"DEBUG","time":"Nov 11, 2018 20:24:11","message":"Sample debug for log file and console"}
//{"severity":"WARNING","time":"Nov 11, 2018 20:24:11","message":"An warning message example"}
//{"severity":"INFO","time":"Nov 11, 2018 20:24:11","message":"An info level message"}
}
有没有想过为什么没有显示来电信息?
根据文档https://godoc.org/go.uber.org/zap#AddCaller 你可以在创建记录器时做这样的事情:
wlogger := zap.New(core, zap.AddCaller())
更新评论回复
您还可以定义调用者编码的实现:
func MyCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(filepath.Base(caller.FullPath()))
}
并将其传递给 cfgConsole 和 cfgFile
AddCallerSkip 可以帮助您跳过调用者的调用堆栈深度。
// before
zap.New(core, zap.AddCaller())
// output: {"level":"panic","time":"2020-06-09T16:08:25.677+0800","line":"log/log.go:118","msg":"before")}
// add caller skip
zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
// output: {"level":"panic","time":"2020-06-09T16:47:39.559+0800","line":"client/client.go:29","msg":"after"}