我正在尝试使用用于配置的自定义编码器将同一条消息同时放入控制台和日志文件。在此过程中,我想显示呼叫者信息,但是即使按照文档中的建议使用了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"}
}
任何人都在想为什么不显示呼叫者信息。
答案 0 :(得分:1)
根据文档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
答案 1 :(得分:1)
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"}