forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpclogger.go
118 lines (91 loc) · 3.02 KB
/
grpclogger.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2017 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package grpc_zap
import (
"fmt"
"go.uber.org/zap"
"google.golang.org/grpc/grpclog"
)
// ReplaceGrpcLogger sets the given zap.Logger as a gRPC-level logger.
// This should be called *before* any other initialization, preferably from init() functions.
// Deprecated: use ReplaceGrpcLoggerV2.
func ReplaceGrpcLogger(logger *zap.Logger) {
zgl := &zapGrpcLogger{logger.With(SystemField, zap.Bool("grpc_log", true))}
grpclog.SetLogger(zgl)
}
type zapGrpcLogger struct {
logger *zap.Logger
}
func (l *zapGrpcLogger) Fatal(args ...interface{}) {
l.logger.Fatal(fmt.Sprint(args...))
}
func (l *zapGrpcLogger) Fatalf(format string, args ...interface{}) {
l.logger.Fatal(fmt.Sprintf(format, args...))
}
func (l *zapGrpcLogger) Fatalln(args ...interface{}) {
l.logger.Fatal(fmt.Sprint(args...))
}
func (l *zapGrpcLogger) Print(args ...interface{}) {
l.logger.Info(fmt.Sprint(args...))
}
func (l *zapGrpcLogger) Printf(format string, args ...interface{}) {
l.logger.Info(fmt.Sprintf(format, args...))
}
func (l *zapGrpcLogger) Println(args ...interface{}) {
l.logger.Info(fmt.Sprint(args...))
}
// ReplaceGrpcLoggerV2 replaces the grpc_log.LoggerV2 with the provided logger.
func ReplaceGrpcLoggerV2(logger *zap.Logger) {
ReplaceGrpcLoggerV2WithVerbosity(logger, 0)
}
// ReplaceGrpcLoggerV2WithVerbosity replaces the grpc_.LoggerV2 with the provided logger and verbosity.
func ReplaceGrpcLoggerV2WithVerbosity(logger *zap.Logger, verbosity int) {
zgl := &zapGrpcLoggerV2{
logger: logger.With(SystemField, zap.Bool("grpc_log", true)),
verbosity: verbosity,
}
grpclog.SetLoggerV2(zgl)
}
type zapGrpcLoggerV2 struct {
logger *zap.Logger
verbosity int
}
func (l *zapGrpcLoggerV2) Info(args ...interface{}) {
l.logger.Info(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Infoln(args ...interface{}) {
l.logger.Info(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Infof(format string, args ...interface{}) {
l.logger.Info(fmt.Sprintf(format, args...))
}
func (l *zapGrpcLoggerV2) Warning(args ...interface{}) {
l.logger.Warn(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Warningln(args ...interface{}) {
l.logger.Warn(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Warningf(format string, args ...interface{}) {
l.logger.Warn(fmt.Sprintf(format, args...))
}
func (l *zapGrpcLoggerV2) Error(args ...interface{}) {
l.logger.Error(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Errorln(args ...interface{}) {
l.logger.Error(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Errorf(format string, args ...interface{}) {
l.logger.Error(fmt.Sprintf(format, args...))
}
func (l *zapGrpcLoggerV2) Fatal(args ...interface{}) {
l.logger.Fatal(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Fatalln(args ...interface{}) {
l.logger.Fatal(fmt.Sprint(args...))
}
func (l *zapGrpcLoggerV2) Fatalf(format string, args ...interface{}) {
l.logger.Fatal(fmt.Sprintf(format, args...))
}
func (l *zapGrpcLoggerV2) V(level int) bool {
return level <= l.verbosity
}