Skip to content

Question: How to map log severity in GCP using Zap SugaredLogger #1110

Answered by vducong
gpkmr-genesys asked this question in Q&A
Discussion options

You must be logged in to vote

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:       "sever…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
3 replies
@cybercent
Comment options

@vducong
Comment options

@kevmo314
Comment options

Answer selected by abhinav
Comment options

You must be logged in to vote
1 reply
@abhinav
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants
Converted from issue

This discussion was converted from issue #1095 on June 15, 2022 12:21.