I am trying to put the same message to console and the log files at the same time with custom encoder for config. In the process I want to display the caller information but the same is not being displayed even if I have used caller
key as suggested in the documentation.
Below is the sample code for the same
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"}
}
Any thoughts why the caller information is not being displayed?
according to documentation https://godoc.org/go.uber.org/zap#AddCaller you can do something like this on logger creation:
wlogger := zap.New(core, zap.AddCaller())
update answer to comment
also you can define your implementation of caller encode:
func MyCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(filepath.Base(caller.FullPath()))
}
and pass it to cfgConsole and cfgFile