-
Notifications
You must be signed in to change notification settings - Fork 13
/
logger.go
366 lines (319 loc) · 8.34 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package log
import (
"fmt"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/sirupsen/logrus"
)
// FieldLogger Wraps the StdLogger, and provides logrus methods for logging with fields.
// Intended to mimic the logrus.FieldLogger interface, but with our own interface and implementation.
type FieldLogger interface {
StdLogger
WithField(key string, value interface{}) FieldLogger
WithFields(fields logrus.Fields) FieldLogger
WithError(err error) FieldLogger
WithComponent(componentName string) FieldLogger
WithPackage(packageName string) FieldLogger
}
// StdLogger interface for logging methods found in the go standard library logger, and logrus methods.
type StdLogger interface {
Redactor
logrus.StdLogger
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Debugln(args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Errorln(args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Infoln(args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
Warnln(args ...interface{})
Trace(args ...interface{})
Tracef(format string, args ...interface{})
Traceln(args ...interface{})
}
// Redactor interface for redacting log messages
type Redactor interface {
DebugRedacted(fields []string, args ...interface{})
ErrorRedacted(fields []string, args ...interface{})
InfoRedacted(fields []string, args ...interface{})
TraceRedacted(fields []string, args ...interface{})
}
// NewFieldLogger returns a FieldLogger for standard logging, and logp logging.
func NewFieldLogger() FieldLogger {
entry := logrus.NewEntry(log)
return &logger{
entry: entry,
}
}
type logger struct {
entry *logrus.Entry
}
// WithComponent adds a field to the log message
func (l *logger) WithComponent(value string) FieldLogger {
return &logger{entry: l.entry.WithField("component", value)}
}
// WithPackage adds a field to the log message
func (l *logger) WithPackage(value string) FieldLogger {
return &logger{entry: l.entry.WithField("package", value)}
}
// WithField adds a field to the log message
func (l *logger) WithField(key string, value interface{}) FieldLogger {
return &logger{entry: l.entry.WithField(key, value)}
}
// WithFields adds multiple fields to the log message
func (l *logger) WithFields(fields logrus.Fields) FieldLogger {
return &logger{entry: l.entry.WithFields(fields)}
}
// WithError adds an error field to the message
func (l *logger) WithError(err error) FieldLogger {
return &logger{entry: l.entry.WithError(err)}
}
// Debugf prints a formatted debug message
func (l *logger) Debugf(format string, args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Named(debugSelector).Debugf(format, args...)
return
}
l.entry.Debugf(format, args...)
}
// Infof prints a formatted info message
func (l *logger) Infof(format string, args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Infof(format, args...)
return
}
l.entry.Infof(format, args...)
}
// Printf formats a message
func (l *logger) Printf(format string, args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Infof(format, args...)
return
}
l.entry.Printf(format, args...)
}
// Warnf prints a formatted warning message
func (l *logger) Warnf(format string, args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Warnf(format, args...)
return
}
l.entry.Warnf(format, args...)
}
// Tracef prints a formatted trace message
func (l *logger) Tracef(format string, args ...interface{}) {
if l.isLogP() && GetLevel() == logrus.TraceLevel {
lgp := l.logpWithEntries()
lgp.Named(traceSelector).Debugf(format, args...)
return
}
l.entry.Tracef(format, args...)
}
// Errorf prints a formatted error message
func (l *logger) Errorf(format string, args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Errorw(format, args...)
return
}
l.entry.Errorf(format, args...)
}
// Fatalf prints a formatted fatal message
func (l *logger) Fatalf(format string, args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Fatalw(format, args...)
return
}
l.entry.Fatalf(format, args...)
}
// Panicf prints a formatted panic message
func (l *logger) Panicf(format string, args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Panicw(format, args...)
return
}
l.entry.Panicf(format, args...)
}
// Debug prints a debug message
func (l *logger) Debug(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Named(debugSelector).Debug(args...)
return
}
l.entry.Debug(args...)
}
// Info prints an info message
func (l *logger) Info(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Info(args...)
return
}
l.entry.Info(args...)
}
// Print prints a message
func (l *logger) Print(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Info(args...)
return
}
l.entry.Print(args...)
}
// Trace prints a trace message
func (l *logger) Trace(args ...interface{}) {
if l.isLogP() && GetLevel() == logrus.TraceLevel {
lgp := l.logpWithEntries()
lgp.Named(traceSelector).Debug(args...)
return
}
l.entry.Trace(args...)
}
// Warn prints a warning message
func (l *logger) Warn(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Warn(args...)
return
}
l.entry.Warn(args...)
}
// Error prints an error message
func (l *logger) Error(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Error(args...)
return
}
l.entry.Error(args...)
}
// Fatal prints a fatal error message
func (l *logger) Fatal(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Fatal(args...)
return
}
l.entry.Fatal(args...)
}
// Panic prints a panic message
func (l *logger) Panic(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Panic(args...)
return
}
l.entry.Panic(args...)
}
// Debugln prints a debug line
func (l *logger) Debugln(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Named(debugSelector).Debug(args...)
return
}
l.entry.Debugln(args...)
}
// Infoln prints an info line
func (l *logger) Infoln(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Info(args...)
return
}
l.entry.Infoln(args...)
}
// Println prints a line
func (l *logger) Println(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Info(args...)
return
}
l.entry.Println(args...)
}
// Traceln prints a trace line
func (l *logger) Traceln(args ...interface{}) {
if l.isLogP() && GetLevel() == logrus.TraceLevel {
lgp := l.logpWithEntries()
lgp.Named(traceSelector).Debug(args...)
return
}
l.entry.Traceln(args...)
}
// Warnln prints a warn line
func (l *logger) Warnln(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Warn(args...)
return
}
l.entry.Warnln(args...)
}
// Errorln prints an error line
func (l *logger) Errorln(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Error(args...)
return
}
l.entry.Errorln(args...)
}
// Fatalln prints a fatal line
func (l *logger) Fatalln(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Fatal(args...)
return
}
l.entry.Fatalln(args...)
}
// Panicln prints a panic line
func (l *logger) Panicln(args ...interface{}) {
if l.isLogP() {
lgp := l.logpWithEntries()
lgp.Panic(args...)
return
}
l.entry.Panicln(args...)
}
// TraceRedacted redacts a trace message
func (l *logger) TraceRedacted(fields []string, args ...interface{}) {
l.Trace(ObscureArguments(fields, args...))
}
// ErrorRedacted redacts an error message
func (l *logger) ErrorRedacted(fields []string, args ...interface{}) {
l.Error(ObscureArguments(fields, args...))
}
// InfoRedacted redacts an info message
func (l *logger) InfoRedacted(fields []string, args ...interface{}) {
l.Info(ObscureArguments(fields, args...))
}
// DebugRedacted redacts a debug message
func (l *logger) DebugRedacted(fields []string, args ...interface{}) {
l.Debug(ObscureArguments(fields, args...))
}
func (l *logger) isLogP() bool {
return isLogP
}
func (l *logger) logpWithEntries() *logp.Logger {
var entries []interface{}
for k, val := range l.entry.Data {
entries = append(entries, logp.String(k, fmt.Sprintf("%v", val)))
}
lgp := logp.L()
for _, entry := range entries {
lgp = lgp.With(entry)
}
return lgp
}