-
Notifications
You must be signed in to change notification settings - Fork 178
/
logging_interceptor.go
25 lines (22 loc) · 1.05 KB
/
logging_interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package rpc
import (
grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog/v2"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/tags"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
func customClientCodeToLevel(c codes.Code) logging.Level {
if c == codes.OK {
// log successful returns as Debug to avoid excessive logging in info mode
return logging.DEBUG
}
return logging.DefaultServerCodeToLevel(c)
}
// LoggingInterceptor creates the logging interceptors to log incoming GRPC request and response (minus the payload body)
func LoggingInterceptor(log zerolog.Logger) []grpc.UnaryServerInterceptor {
tagsInterceptor := tags.UnaryServerInterceptor(tags.WithFieldExtractor(tags.CodeGenRequestFieldExtractor))
loggingInterceptor := logging.UnaryServerInterceptor(grpczerolog.InterceptorLogger(log), logging.WithLevels(customClientCodeToLevel))
return []grpc.UnaryServerInterceptor{tagsInterceptor, loggingInterceptor}
}