-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrapper.go
319 lines (287 loc) · 7.63 KB
/
wrapper.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package log
import (
"fmt"
"log"
"os"
"sort"
"testing"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
)
/* Zap wrapper */
func NewZapWrapper(logger *zap.Logger) WrapperFactoryFunc {
return func() Wrapper {
return &ZapWrapper{logger: logger, sugar: logger.Sugar()}
}
}
type ZapWrapper struct {
logger *zap.Logger
sugar *zap.SugaredLogger
}
func (l *ZapWrapper) GetLevel() Level {
switch {
case l.logger.Core().Enabled(zap.DebugLevel):
return LevelDebug
case l.logger.Core().Enabled(zap.InfoLevel):
return LevelInfo
case l.logger.Core().Enabled(zap.WarnLevel):
return LevelWarn
case l.logger.Core().Enabled(zap.ErrorLevel):
return LevelError
case l.logger.Core().Enabled(zap.FatalLevel):
return LevelFatal
case l.logger.Core().Enabled(zap.PanicLevel):
return LevelError
default:
panic(fmt.Errorf("zap level is not handled"))
}
}
func (l *ZapWrapper) WithField(key string, value interface{}) {
l.sugar = l.sugar.With(key, value)
}
func (l *ZapWrapper) format(format string, args ...interface{}) string {
var msg = format
if len(args) > 0 {
msg = fmt.Sprintf(format, args...)
}
return msg
}
func (l *ZapWrapper) Debugf(format string, args ...interface{}) {
l.sugar.Debugw(l.format(format, args...))
}
func (l *ZapWrapper) Infof(format string, args ...interface{}) {
l.sugar.Infow(l.format(format, args...))
}
func (l *ZapWrapper) Warnf(format string, args ...interface{}) {
l.sugar.Warnw(l.format(format, args...))
}
func (l *ZapWrapper) Fatalf(format string, args ...interface{}) {
l.sugar.Fatalw(l.format(format, args...))
}
func (l *ZapWrapper) Errorf(format string, args ...interface{}) {
l.sugar.Errorw(l.format(format, args...))
}
func (l *ZapWrapper) Panicf(format string, args ...interface{}) {
l.sugar.Panicw(l.format(format, args...))
}
/* Logrus wrapper */
func NewLogrusWrapper(logger *logrus.Logger) WrapperFactoryFunc {
return func() Wrapper {
return &LogrusWrapper{entry: logrus.NewEntry(logger)}
}
}
type LogrusWrapper struct {
entry *logrus.Entry
}
func (l *LogrusWrapper) GetLevel() Level {
switch l.entry.Logger.Level {
case logrus.DebugLevel:
return LevelDebug
case logrus.InfoLevel:
return LevelInfo
case logrus.WarnLevel:
return LevelWarn
case logrus.ErrorLevel:
return LevelError
case logrus.FatalLevel:
return LevelFatal
case logrus.PanicLevel:
return LevelPanic
default:
panic(fmt.Errorf("logrus level %q is not handled", l.entry.Level))
}
}
func (l *LogrusWrapper) WithField(key string, value interface{}) {
l.entry = l.entry.WithField(key, value)
}
func (l *LogrusWrapper) Debugf(format string, args ...interface{}) {
if len(args) == 0 {
l.entry.Debug(format)
} else {
l.entry.Debugf(format, args...)
}
}
func (l *LogrusWrapper) Infof(format string, args ...interface{}) {
if len(args) == 0 {
l.entry.Info(format)
} else {
l.entry.Infof(format, args...)
}
}
func (l *LogrusWrapper) Warnf(format string, args ...interface{}) {
if len(args) == 0 {
l.entry.Warn(format)
} else {
l.entry.Warnf(format, args...)
}
}
func (l *LogrusWrapper) Fatalf(format string, args ...interface{}) {
if len(args) == 0 {
l.entry.Fatal(format)
} else {
l.entry.Fatalf(format, args...)
}
}
func (l *LogrusWrapper) Errorf(format string, args ...interface{}) {
if len(args) == 0 {
l.entry.Error(format)
} else {
l.entry.Errorf(format, args...)
}
}
func (l *LogrusWrapper) Panicf(format string, args ...interface{}) {
if len(args) == 0 {
l.entry.Panic(format)
} else {
l.entry.Panicf(format, args...)
}
}
/* testing.T wrapper */
func NewTestingWrapper(t testing.TB) WrapperFactoryFunc {
return func() Wrapper {
return &TestingWrapper{t: t}
}
}
type TestingWrapper struct {
ctx map[string]string
t testing.TB
}
func (l *TestingWrapper) GetLevel() Level {
return LevelDebug
}
func (l *TestingWrapper) WithField(key string, value interface{}) {
if l.ctx == nil {
l.ctx = map[string]string{}
}
l.ctx[key] = fmt.Sprintf("%v", value)
}
func formatCtx(ctx map[string]string) string {
var keys []string
for k := range ctx {
keys = append(keys, k)
}
sort.Strings(keys)
var s string
for _, k := range keys {
v := ctx[k]
s += fmt.Sprintf("[%s=%s]", k, v)
}
return s
}
func (l *TestingWrapper) log(level, format string, args ...interface{}) {
// Recover function to avoid panic: Log in goroutine after test has completed
defer func() {
if r := recover(); r != nil {
fmt.Printf("[" + level + "] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}()
if len(args) == 0 {
l.t.Log("[" + level + "] " + formatCtx(l.ctx) + " " + format)
} else {
l.t.Logf("[" + level + "] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}
func (l *TestingWrapper) fatal(format string, args ...interface{}) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("[FATAL] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
os.Exit(2)
}
}()
if len(args) == 0 {
l.t.Fatal("[FATAL] " + formatCtx(l.ctx) + " " + format)
} else {
l.t.Fatalf("[FATAL] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}
func (l *TestingWrapper) Debugf(format string, args ...interface{}) {
l.log("DEBUG", format, args...)
}
func (l *TestingWrapper) Infof(format string, args ...interface{}) {
l.log("INFO", format, args...)
}
func (l *TestingWrapper) Warnf(format string, args ...interface{}) {
l.log("WARN", format, args...)
}
func (l *TestingWrapper) Fatalf(format string, args ...interface{}) {
l.fatal(format, args...)
}
func (l *TestingWrapper) Errorf(format string, args ...interface{}) {
l.log("ERROR", format, args...)
}
func (l *TestingWrapper) Panicf(format string, args ...interface{}) {
l.log("PANIC", format, args...)
}
/* golang log package wrapper */
type StdWrapperOptions struct {
Level Level
DisableTimestamp bool
}
type StdWrapper struct {
opts StdWrapperOptions
ctx map[string]string
}
func NewStdWrapper(opts StdWrapperOptions) WrapperFactoryFunc {
return func() Wrapper {
return &StdWrapper{opts: opts}
}
}
func (l *StdWrapper) GetLevel() Level {
return l.opts.Level
}
func (l *StdWrapper) WithField(key string, value interface{}) {
if l.ctx == nil {
l.ctx = map[string]string{}
}
l.ctx[key] = fmt.Sprintf("%v", value)
}
func (l *StdWrapper) Print(s string) {
if l.opts.DisableTimestamp {
fmt.Println(s)
} else {
log.Println(s)
}
}
func (l *StdWrapper) Debugf(format string, args ...interface{}) {
if len(args) == 0 {
l.Print("[DEBUG] " + formatCtx(l.ctx) + " " + format)
} else {
l.Print("[DEBUG] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}
func (l *StdWrapper) Infof(format string, args ...interface{}) {
if len(args) == 0 {
l.Print("[INFO] " + formatCtx(l.ctx) + " " + format)
} else {
l.Print("[INFO] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}
func (l *StdWrapper) Warnf(format string, args ...interface{}) {
if len(args) == 0 {
l.Print("[WARN] " + formatCtx(l.ctx) + " " + format)
} else {
l.Print("[WARN] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}
func (l *StdWrapper) Fatalf(format string, args ...interface{}) {
if len(args) == 0 {
l.Print("[FATAL] " + formatCtx(l.ctx) + " " + format)
} else {
l.Print("[FATAL] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
os.Exit(1)
}
func (l *StdWrapper) Errorf(format string, args ...interface{}) {
if len(args) == 0 {
l.Print("[ERROR] " + formatCtx(l.ctx) + " " + format)
} else {
l.Print("[ERROR] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}
func (l *StdWrapper) Panicf(format string, args ...interface{}) {
if len(args) == 0 {
l.Print("[PANIC] " + formatCtx(l.ctx) + " " + format)
} else {
l.Print("[PANIC] " + formatCtx(l.ctx) + " " + fmt.Sprintf(format, args...))
}
}