我正在使用下面的代码使用Uber zap logger记录控制台上的日志和日志文件。我正在寻找如何拥有自定义消息编码器,以便消息的输出格式可以如下
{"severity":"DEBUG","message":"Dec 12, 2018 19:52:39 [log.go:77] Sample debug for log file and console"}
下面是我用来在控制台上转储日志的代码。
package main
import (
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"path/filepath"
)
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 funcCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(filepath.Base(caller.FullPath()))
}
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: funcCaller,
}
cfgFile := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "severity",
EncodeLevel: CustomLevelFileEncoder,
TimeKey: "time",
EncodeTime: SyslogTimeEncoder,
CallerKey: "caller",
EncodeCaller: funcCaller,
}
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, zap.AddCaller())
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, zap.AddCaller())
flogger.Debug("An exclusive message for file")
//output
//{"severity":"DEBUG","time":"Dec 12, 2018 19:52:39","caller":"log.go:77","message":"Sample debug for log file and console"}
}
任何人都在考虑如何实现预期的输出,因为我们需要将消息以上述格式放在控制台上。
答案 0 :(得分:0)
根据您的代码,以下配置对我有效:
func logInit(d bool, f *os.File) *zap.SugaredLogger {
pe := zap.NewProductionEncoderConfig()
fileEncoder := zapcore.NewJSONEncoder(pe)
pe.EncodeTime = zapcore.ISO8601TimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(pe)
level := zap.InfoLevel
if d {
level = zap.DebugLevel
}
core := zapcore.NewTee(
zapcore.NewCore(fileEncoder, zapcore.AddSync(f), level),
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), level),
)
l := zap.New(core)
return l.Sugar()
}