/
logger.go
38 lines (31 loc) · 1.08 KB
/
logger.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
26
27
28
29
30
31
32
33
34
35
36
37
38
package middleware
import (
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
func codeToLevel(code codes.Code) zapcore.Level {
if code == codes.OK {
return zap.DebugLevel
}
return grpc_zap.DefaultCodeToLevel(code)
}
func AddLogging(logger *zap.Logger, opts []grpc.ServerOption) []grpc.ServerOption {
o := []grpc_zap.Option{
grpc_zap.WithLevels(codeToLevel),
}
grpc_zap.ReplaceGrpcLogger(logger)
opts = append(opts, grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zap.UnaryServerInterceptor(logger, o...),
))
opts = append(opts, grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zap.StreamServerInterceptor(logger, o...),
))
return opts
}