-
Notifications
You must be signed in to change notification settings - Fork 4
/
suplog.go
431 lines (352 loc) · 9.71 KB
/
suplog.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package suplog
import (
"context"
"io"
"os"
"strings"
"sync"
"time"
debugHook "github.com/xlab/suplog/hooks/debug"
"github.com/sirupsen/logrus"
"github.com/xlab/closer"
"github.com/xlab/suplog/stackcache"
)
// NewLogger constructs a new suplogger. The default formatter would be TextFormatter
// if not overrident by and env variable.
func NewLogger(wr io.Writer, formatter Formatter, hooks ...Hook) Logger {
if formatter == nil {
switch os.Getenv("LOG_FORMATTER") {
case "json":
formatter = new(JSONFormatter)
default:
formatter = new(TextFormatter)
}
}
log := &suplogger{
logger: &logrus.Logger{
Out: wr,
Formatter: formatter,
Hooks: make(LevelHooks),
Level: DebugLevel,
ExitFunc: closer.Exit,
},
writer: wr,
mux: new(sync.Mutex),
stackTraceOffset: 0,
initDone: true,
}
log.reloadStackTraceCache()
log.entry = log.logger.WithContext(context.Background())
for _, h := range hooks {
log.AddHook(h)
}
return log
}
type suplogger struct {
logger *logrus.Logger
entry *logrus.Entry
mux *sync.Mutex
writer io.Writer
stack stackcache.StackCache
stackTraceOffset int
init sync.Once
initDone bool
closed bool
}
func (l *suplogger) initOnce() {
l.init.Do(func() {
if l.initDone {
// bail out if init already done (if New contstructor has been used).
return
}
if l.writer == nil {
l.writer = os.Stderr
}
var formatter Formatter
switch os.Getenv("LOG_FORMATTER") {
case "json":
formatter = new(JSONFormatter)
default:
formatter = new(TextFormatter)
}
// otherwise init output with conservative defaults
l.logger = &logrus.Logger{
Out: l.writer,
Formatter: formatter,
Hooks: make(LevelHooks),
Level: DebugLevel,
ExitFunc: closer.Exit,
}
l.entry = l.logger.WithContext(context.Background())
l.reloadStackTraceCache()
l.addDefaultHooks()
l.mux = new(sync.Mutex)
l.initDone = true
})
}
const defaultStackSearchOffset = 1
// reloadStackTraceCache allows to reload the stack trace reporter with new offset,
// allowing to wrap suplogger into other funcs.
func (l *suplogger) reloadStackTraceCache() {
l.stack = stackcache.New(defaultStackSearchOffset, l.stackTraceOffset, "github.com/xlab/suplog")
}
// addDefaultHooks initializes default hooks and additional hooks
// based on the environment setup.
func (l *suplogger) addDefaultHooks() {
// new logger with same out and formatter, but no hooks.
// used to avoid hooking a hooka-roo from hooks,
// that hits a mutex in the same logrus entry.
hookLogger := NewLogger(l.logger.Out, l.logger.Formatter)
l.logger.AddHook(debugHook.NewHook(hookLogger, nil))
// This has been there for ages, but makes no sense in long run,
// also adds too much dependencies into the go mod.
//
// if isTrue(os.Getenv("LOG_BLOB_ENABLED")) {
// l.logger.AddHook(blobHook.NewHook(hookLogger, nil))
// }
//
// if isTrue(os.Getenv("LOG_BUGSNAG_ENABLED")) {
// l.logger.AddHook(bugsnagHook.NewHook(hookLogger, nil))
// }
}
// Adds a field to the log entry, note that it doesn't log until you call
// Debug, Print, Info, Warn, Error, Fatal or Panic. It only creates a log entry.
// If you want multiple fields, use `WithFields`.
func (l *suplogger) WithField(key string, value interface{}) Logger {
l.initOnce()
outCopy := l.copy()
outCopy.entry = l.entry.WithField(key, value)
return outCopy
}
// Adds a struct of fields to the log entry. All it does is call `WithField` for
// each `Field`.
func (l *suplogger) WithFields(fields Fields) Logger {
l.initOnce()
outCopy := l.copy()
outCopy.entry = l.entry.WithFields(fields)
return outCopy
}
// Add an error as single field to the log entry. All it does is call
// `WithError` for the given `error`.
func (l *suplogger) WithError(err error) Logger {
l.initOnce()
outCopy := l.copy()
outCopy.entry = l.entry.WithError(err)
return outCopy
}
// Add a context to the log entry.
func (l *suplogger) WithContext(ctx context.Context) Logger {
l.initOnce()
outCopy := l.copy()
outCopy.entry = l.entry.WithContext(ctx)
return outCopy
}
// Overrides the time of the log entry.
func (l *suplogger) WithTime(t time.Time) Logger {
l.initOnce()
outCopy := l.copy()
outCopy.entry = l.entry.WithTime(t)
return outCopy
}
func (l *suplogger) Logf(level Level, format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(level, format, args...)
}
func (l *suplogger) Tracef(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(TraceLevel, format, args...)
}
func (l *suplogger) Debugf(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(DebugLevel, format, args...)
}
func (l *suplogger) Infof(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(InfoLevel, format, args...)
}
func (l *suplogger) Printf(format string, args ...interface{}) {
l.initOnce()
l.entry.Printf(format, args...)
}
func (l *suplogger) Warningf(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(WarnLevel, format, args...)
}
func (l *suplogger) Errorf(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(ErrorLevel, format, args...)
}
func (l *suplogger) Fatalf(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(FatalLevel, format, args...)
l.logger.Exit(1)
}
func (l *suplogger) Panicf(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(PanicLevel, format, args...)
}
func (l *suplogger) Log(level Level, args ...interface{}) {
l.initOnce()
l.entry.Log(level, args...)
}
func (l *suplogger) Trace(args ...interface{}) {
l.initOnce()
l.entry.Log(TraceLevel, args...)
}
func (l *suplogger) Info(args ...interface{}) {
l.initOnce()
l.entry.Log(InfoLevel, args...)
}
func (l *suplogger) Print(args ...interface{}) {
l.initOnce()
l.entry.Print(args...)
}
func (l *suplogger) Fatal(args ...interface{}) {
l.initOnce()
l.entry.Log(FatalLevel, args...)
l.logger.Exit(1)
}
func (l *suplogger) Panic(args ...interface{}) {
l.initOnce()
l.entry.Log(PanicLevel, args...)
}
func (l *suplogger) Logln(level Level, args ...interface{}) {
l.initOnce()
l.entry.Logln(level, args...)
}
func (l *suplogger) Traceln(args ...interface{}) {
l.initOnce()
l.entry.Logln(TraceLevel, args...)
}
func (l *suplogger) Debugln(args ...interface{}) {
l.initOnce()
l.entry.Logln(DebugLevel, args...)
}
func (l *suplogger) Infoln(args ...interface{}) {
l.initOnce()
l.entry.Logln(InfoLevel, args...)
}
func (l *suplogger) Println(args ...interface{}) {
l.initOnce()
l.entry.Println(args...)
}
func (l *suplogger) Warningln(args ...interface{}) {
l.initOnce()
l.entry.Logln(WarnLevel, args...)
}
func (l *suplogger) Errorln(args ...interface{}) {
l.initOnce()
l.entry.Logln(ErrorLevel, args...)
}
func (l *suplogger) Fatalln(args ...interface{}) {
l.initOnce()
l.entry.Logln(FatalLevel, args...)
l.logger.Exit(1)
}
func (l *suplogger) Debug(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(DebugLevel, format, args...)
}
func (l *suplogger) Notification(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(InfoLevel, format, args...)
}
func (l *suplogger) Success(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(InfoLevel, format, args...)
}
func (l *suplogger) Warning(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(WarnLevel, format, args...)
}
func (l *suplogger) Error(format string, args ...interface{}) {
l.initOnce()
l.entry.Logf(ErrorLevel, format, args...)
}
func (l *suplogger) Panicln(args ...interface{}) {
l.initOnce()
l.entry.Logln(PanicLevel, args...)
}
// SetLevel sets the logger level.
func (l *suplogger) SetLevel(level Level) {
l.initOnce()
l.logger.SetLevel(level)
}
// GetLevel returns the logger level.
func (l *suplogger) GetLevel() Level {
l.initOnce()
return l.logger.GetLevel()
}
// AddHook adds a hook to the logger hooks.
func (l *suplogger) AddHook(hook Hook) {
l.initOnce()
l.logger.AddHook(hook)
}
// IsLevelEnabled checks if the log level of the logger is greater than the level param
func (l *suplogger) IsLevelEnabled(level Level) bool {
l.initOnce()
return l.logger.IsLevelEnabled(level)
}
// SetFormatter sets the logger formatter.
func (l *suplogger) SetFormatter(formatter Formatter) {
l.initOnce()
l.logger.SetFormatter(formatter)
}
// SetFormatter sets the logger formatter.
func (l *suplogger) SetStackTraceOffset(offset int) {
l.initOnce()
l.stackTraceOffset = offset
l.reloadStackTraceCache()
}
// SetOutput sets the logger suplog.
func (l *suplogger) SetOutput(output io.Writer) {
l.initOnce()
l.logger.SetOutput(output)
}
// ReplaceHooks replaces the logger hooks and returns the old ones
func (l *suplogger) ReplaceHooks(hooks LevelHooks) LevelHooks {
l.initOnce()
return l.logger.ReplaceHooks(hooks)
}
// Close effectively closes output, closing the underlying writer
// if it implements io.WriteCloser.
func (l *suplogger) Close() (err error) {
// bail out if already closed
l.mux.Lock()
defer l.mux.Unlock()
if l.closed {
return
}
l.closed = true
// try to close only WriteClosers
if outCloser, ok := l.writer.(io.WriteCloser); ok {
return outCloser.Close()
}
return
}
// CallerName returns caller function name.
func (l *suplogger) CallerName() string {
l.initOnce()
caller := l.stack.GetCaller()
parts := strings.Split(caller.Function, "/")
nameParts := strings.Split(parts[len(parts)-1], ".")
return nameParts[len(nameParts)-1]
}
func isTrue(v string) bool {
switch strings.ToLower(v) {
case "1", "true", "y":
return true
}
return false
}
// copy allows to construct an suplogger copy with new entry.
func (l *suplogger) copy() *suplogger {
return &suplogger{
writer: l.writer,
logger: l.logger,
stack: l.stack,
mux: l.mux,
initDone: l.initDone,
closed: l.closed,
}
}