-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
213 lines (190 loc) · 6.7 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package grpcwrap
import (
"fmt"
"os"
"runtime"
"github.com/DataWorkbench/glog"
"google.golang.org/grpc/grpclog"
)
const (
// infoLevel indicates Info severity.
infoLevel int = iota + 1
// warningLevel indicates Warning severity.
warningLevel
// errorLevel indicates Error severity.
errorLevel
// fatalLevel indicates Fatal severity.
fatalLevel
)
// SetLogger sets logger object that used in grpc.
// Not mutex-protected, should be called before any gRPC functions.
func SetLogger(output *glog.Logger, cfg *LogConfig) {
grpclog.SetLoggerV2(&loggerT{
output: output,
cfg: cfg,
})
}
type LogConfig struct {
// grpc log level: 1 => info, 2 => waring, 3 => error, 4 => fatal
Level int `json:"level" yaml:"level" env:"LEVEL,default=3" validate:"gte=1,lte=4"`
Verbosity int `json:"verbosity" yaml:"verbosity" env:"VERBOSITY,default=99" validate:"required"`
}
// loggerT for implements interface{} grpclog.LoggerV2 and grpclog.DepthLoggerV2
type loggerT struct {
output *glog.Logger
cfg *LogConfig
}
// implements grpclog.LoggerV2
//
// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
func (g *loggerT) Info(args ...interface{}) {
if infoLevel < g.cfg.Level {
return
}
g.output.Info().RawString("grpclog-Info", fmt.Sprint(args...)).Fire()
}
// Infoln logs to INFO log. Arguments are handled in the manner of fmt.Println.
func (g *loggerT) Infoln(args ...interface{}) {
if infoLevel < g.cfg.Level {
return
}
g.output.Info().RawString("grpclog-Infoln", fmt.Sprint(args...)).Fire()
}
// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
func (g *loggerT) Infof(format string, args ...interface{}) {
if infoLevel < g.cfg.Level {
return
}
g.output.Info().RawString("grpclog-Infof", fmt.Sprintf(format, args...)).Fire()
}
// Warning logs to WARNING log. Arguments are handled in the manner of fmt.Print.
func (g *loggerT) Warning(args ...interface{}) {
if warningLevel < g.cfg.Level {
return
}
g.output.Warn().RawString("grpclog-Warning", fmt.Sprint(args...)).Fire()
}
// Warningln logs to WARNING log. Arguments are handled in the manner of fmt.Println.
func (g *loggerT) Warningln(args ...interface{}) {
if warningLevel < g.cfg.Level {
return
}
g.output.Warn().RawString("grpclog-Warningln", fmt.Sprint(args...)).Fire()
}
// Warningf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
func (g *loggerT) Warningf(format string, args ...interface{}) {
if warningLevel < g.cfg.Level {
return
}
g.output.Warn().RawString("grpclog-Warningf", fmt.Sprintf(format, args...)).Fire()
}
// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func (g *loggerT) Error(args ...interface{}) {
if errorLevel < g.cfg.Level {
return
}
g.output.Error().RawString("grpclog-Error", fmt.Sprint(args...)).Fire()
}
// Errorln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
func (g *loggerT) Errorln(args ...interface{}) {
if errorLevel < g.cfg.Level {
return
}
g.output.Error().RawString("grpclog-Errorln", fmt.Sprint(args...)).Fire()
}
// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func (g *loggerT) Errorf(format string, args ...interface{}) {
if errorLevel < g.cfg.Level {
return
}
g.output.Error().RawString("grpclog-Errorf", fmt.Sprintf(format, args...)).Fire()
}
// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
// gRPC ensures that all Fatal logs will exit with os.Exit(1).
// Implementations may also call os.Exit() with a non-zero exit code.
func (g *loggerT) Fatal(args ...interface{}) {
if fatalLevel < g.cfg.Level {
return
}
g.output.Fatal().RawString("grpclog-Fatal", fmt.Sprint(args...)).Fire()
os.Exit(1)
}
// Fatalln logs to ERROR log. Arguments are handled in the manner of fmt.Println.
// gRPC ensures that all Fatal logs will exit with os.Exit(1).
// Implementations may also call os.Exit() with a non-zero exit code.
func (g *loggerT) Fatalln(args ...interface{}) {
if fatalLevel < g.cfg.Level {
return
}
g.output.Fatal().RawString("grpclog-Fatalln", fmt.Sprint(args...)).Fire()
os.Exit(1)
}
// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
// gRPC ensures that all Fatal logs will exit with os.Exit(1).
// Implementations may also call os.Exit() with a non-zero exit code.
func (g *loggerT) Fatalf(format string, args ...interface{}) {
if fatalLevel < g.cfg.Level {
return
}
g.output.Fatal().RawString("grpclog-Fatalf", fmt.Sprintf(format, args...)).Fire()
os.Exit(1)
}
// V reports whether verbosity level l is at least the requested verbose level.
func (g *loggerT) V(l int) bool {
return l <= g.cfg.Verbosity
}
// implements grpclog.DepthLoggerV2
//
// InfoDepth logs to INFO log at the specified depth. Arguments are handled in the manner of fmt.Print.
func (g *loggerT) InfoDepth(depth int, args ...interface{}) {
if infoLevel < g.cfg.Level {
return
}
_, file, line, ok := runtime.Caller(depth + 2)
if ok {
caller := fmt.Sprintf("%s:%d", file, line)
g.output.Info().RawString("grpclog-InfoDepth", fmt.Sprint(args...)).RawString("caller", caller).Fire()
} else {
g.output.Info().RawString("grpclog-InfoDepth", fmt.Sprint(args...)).Fire()
}
}
// WarningDepth logs to WARNING log at the specified depth. Arguments are handled in the manner of fmt.Print.
func (g *loggerT) WarningDepth(depth int, args ...interface{}) {
if warningLevel < g.cfg.Level {
return
}
_, file, line, ok := runtime.Caller(depth + 2)
if ok {
caller := fmt.Sprintf("%s:%d", file, line)
g.output.Warn().RawString("grpclog-WarningDepth", fmt.Sprint(args...)).RawString("caller", caller).Fire()
} else {
g.output.Warn().RawString("grpclog-WarningDepth", fmt.Sprint(args...)).Fire()
}
}
// ErrorDetph logs to ERROR log at the specified depth. Arguments are handled in the manner of fmt.Print.
func (g *loggerT) ErrorDepth(depth int, args ...interface{}) {
if errorLevel < g.cfg.Level {
return
}
_, file, line, ok := runtime.Caller(depth + 2)
if ok {
caller := fmt.Sprintf("%s:%d", file, line)
g.output.Error().RawString("grpclog-ErrorDepth", fmt.Sprint(args...)).RawString("caller", caller).Fire()
} else {
g.output.Error().RawString("grpclog-ErrorDepth", fmt.Sprint(args...)).Fire()
}
}
// FatalDepth logs to FATAL log at the specified depth. Arguments are handled in the manner of fmt.Print.
func (g *loggerT) FatalDepth(depth int, args ...interface{}) {
if fatalLevel < g.cfg.Level {
return
}
_, file, line, ok := runtime.Caller(depth + 2)
if ok {
caller := fmt.Sprintf("%s:%d", file, line)
g.output.Fatal().RawString("grpclog-FatalDepth", fmt.Sprint(args...)).RawString("caller", caller).Fire()
} else {
g.output.Fatal().RawString("grpclog-FatalDepth", fmt.Sprint(args...)).Fire()
}
os.Exit(1)
}