grpc-ecosystem / go-grpc-middleware

Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more.
Apache License 2.0
6.31k stars 692 forks source link

Q: Logger migration #685

Closed loeffel-io closed 11 months ago

loeffel-io commented 11 months ago

Hello!

i am curious how i would migrate our zap gcloud implementation from v2.0.0-rc3 to v2.0.1 - i am thankful for every help!

func NewZap(development bool) (*zap.Logger, error) {
    var err error
    var logger *zap.Logger

    var loggerCfg = &zap.Config{
        Level:       zap.NewAtomicLevelAt(zapcore.InfoLevel),
        Development: development,
        Encoding:    "json",
        EncoderConfig: zapcore.EncoderConfig{
            TimeKey:       "time",
            LevelKey:      "severity",
            NameKey:       "logger",
            CallerKey:     "caller",
            MessageKey:    "message",
            StacktraceKey: "stacktrace",
            LineEnding:    zapcore.DefaultLineEnding,
            EncodeLevel: 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")
                }
            },
            EncodeTime:     zapcore.RFC3339TimeEncoder,
            EncodeDuration: zapcore.MillisDurationEncoder,
            EncodeCaller:   zapcore.ShortCallerEncoder,
        },
        OutputPaths:      []string{"stdout"},
        ErrorOutputPaths: []string{"stderr"},
    }

    if logger, err = loggerCfg.Build(zap.AddStacktrace(zap.DPanicLevel)); err != nil {
        return nil, err
    }

    return logger, nil
}
logging.StreamServerInterceptor(grpczap.InterceptorLogger(logger)),
johanbrandhorst commented 11 months ago

There's an example, does that help? https://github.com/grpc-ecosystem/go-grpc-middleware/blob/main/interceptors/logging/examples/zap/example_test.go

loeffel-io commented 11 months ago

Yeah that works perfectly - was just curious if the lvls matches Thanks @johanbrandhorst