github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/logging_interceptor.go (about) 1 package rpc 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" 8 "github.com/rs/zerolog" 9 "google.golang.org/grpc" 10 "google.golang.org/grpc/codes" 11 ) 12 13 func customClientCodeToLevel(c codes.Code) logging.Level { 14 if c == codes.OK { 15 // log successful returns as Debug to avoid excessive logging in info mode 16 return logging.LevelDebug 17 } 18 return logging.DefaultServerCodeToLevel(c) 19 } 20 21 // LoggingInterceptor creates the logging interceptors to log incoming GRPC request and response (minus the payload body) 22 func LoggingInterceptor(log zerolog.Logger) []grpc.UnaryServerInterceptor { 23 loggingInterceptor := logging.UnaryServerInterceptor(InterceptorLogger(log), logging.WithLevels(customClientCodeToLevel)) 24 return []grpc.UnaryServerInterceptor{loggingInterceptor} 25 } 26 27 // InterceptorLogger adapts zerolog logger to interceptor logger. 28 // This code is simple enough to be copied and not imported. 29 func InterceptorLogger(l zerolog.Logger) logging.Logger { 30 return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { 31 l := l.With().Fields(fields).Logger() 32 33 switch lvl { 34 case logging.LevelDebug: 35 l.Debug().Msg(msg) 36 case logging.LevelInfo: 37 l.Info().Msg(msg) 38 case logging.LevelWarn: 39 l.Warn().Msg(msg) 40 case logging.LevelError: 41 l.Error().Msg(msg) 42 default: 43 panic(fmt.Sprintf("unknown level %v", lvl)) 44 } 45 }) 46 }