Question: How to map log severity in GCP using Zap SugaredLogger #1110
Answered
by
vducong
gpkmr-genesys
asked this question in
Q&A
-
Beta Was this translation helpful? Give feedback.
Answered by
vducong
Jun 8, 2022
Replies: 2 comments 4 replies
-
To log GCP severity correctly, first, you have to use JSON encoding, then map Zap's level with GCP severity and set appropriate keys with the LogEntry as documented here For example: func New(cfg *configs.Config) *Logger {
loggerCfg := &zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
Encoding: "json",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
plain, err := loggerCfg.Build(zap.AddStacktrace(zap.DPanicLevel))
if err != nil {
plain = zap.NewNop()
}
return plain.Sugar()
}
var encoderConfig = zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "severity",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: encodeLevel(),
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.MillisDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
func encodeLevel() zapcore.LevelEncoder {
return func(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
switch l {
case zapcore.DebugLevel:
enc.AppendString("DEBUG")
case zapcore.InfoLevel:
enc.AppendString("INFO")
case zapcore.WarnLevel:
enc.AppendString("WARNING")
case zapcore.ErrorLevel:
enc.AppendString("ERROR")
case zapcore.DPanicLevel:
enc.AppendString("CRITICAL")
case zapcore.PanicLevel:
enc.AppendString("ALERT")
case zapcore.FatalLevel:
enc.AppendString("EMERGENCY")
}
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
abhinav
-
Shall we close this issue? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To log GCP severity correctly, first, you have to use JSON encoding, then map Zap's level with GCP severity and set appropriate keys with the LogEntry as documented here
For example: