-
Notifications
You must be signed in to change notification settings - Fork 25
/
log.go
630 lines (527 loc) · 18.1 KB
/
log.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package log
import (
"fmt"
"io"
stdlog "log"
"os"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"github.com/fluxninja/aperture/pkg/info"
)
const (
// DebugLevel defines debug log level.
DebugLevel = zerolog.DebugLevel
// InfoLevel defines info log level.
InfoLevel = zerolog.InfoLevel
// WarnLevel defines warn log level.
WarnLevel = zerolog.WarnLevel
// ErrorLevel defines error log level.
ErrorLevel = zerolog.ErrorLevel
// FatalLevel defines fatal log level.
FatalLevel = zerolog.FatalLevel
// PanicLevel defines panic log level.
PanicLevel = zerolog.PanicLevel
// NoLevel defines an absent log level.
NoLevel = zerolog.NoLevel
// Disabled disables the logger.
Disabled = zerolog.Disabled
// TraceLevel defines trace log level.
TraceLevel = zerolog.TraceLevel
)
const (
// FlushWait is the amount of time to wait for the buffer to flush.
flushWait = 1000 * time.Millisecond
// DefaultLevel sets info log level, InfoLevel, as default.
defaultLevel = "info"
// ServiceKey is a field key that are used with Service name value as a string to the logger context.
serviceKey = "service"
// ComponentKey is a field key that are used with Component name value as a string to the logger context.
componentKey = "component"
// Sampled is a field key that are used with Sampled value as a bool to the logger context.
sampledKey = "sampled"
)
// Logger is wrapper around zerolog.Logger and io.writers.
type Logger struct {
logger *zerolog.Logger
}
var global *Logger
// Always create a global logger instance.
func init() {
zerolog.TimeFieldFormat = time.RFC3339
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog.CallerMarshalFunc = func(_ uintptr, file string, line int) string {
// short caller format
short := strings.Split(file, "/")
// return the last n elements of the file path
n := 3
if len(short) < n {
n = len(short)
}
path := strings.Join(short[len(short)-n:], "/")
return path + fmt.Sprintf("%d", line)
}
// Create global logger
SetGlobalLogger(NewDefaultLogger())
}
// NewDefaultLogger creates a new default logger with default settings.
func NewDefaultLogger() *Logger {
return NewLogger(os.Stderr, defaultLevel)
}
// SetGlobalLogger closes the previous global logger and sets given logger as a new global logger.
func SetGlobalLogger(lg *Logger) {
global = lg
}
// SetStdLogger sets output for the standard logger.
func SetStdLogger(lg *Logger) {
stdlog.SetFlags(0)
stdlog.SetOutput(lg.logger)
}
// NewLogger creates a new logger.
func NewLogger(w io.Writer, levelString string) *Logger {
level, err := zerolog.ParseLevel(levelString)
if err != nil {
log.Panic().Err(err).Str("level", level.String()).Msg("Unable to parse logger level")
}
zerolog := zerolog.New(w).Level(level).With().Timestamp().Caller().Str(serviceKey, info.Service).Logger()
logger := &Logger{
logger: &zerolog,
}
return logger
}
// WaitFlush waits a few ms to let the the buffer to flush.
func WaitFlush() {
time.Sleep(flushWait)
}
// GetPrettyConsoleWriter returns a pretty console writer.
func GetPrettyConsoleWriter() io.Writer {
output := zerolog.NewConsoleWriter()
return output
}
/* Wrappers around zerolog */
// SetGlobalLevel sets the global log level with given level.
func SetGlobalLevel(level zerolog.Level) {
zerolog.SetGlobalLevel(level)
}
// SetGlobalLevelString parses given levelString and sets the global log level.
func SetGlobalLevelString(levelString string) error {
level, err := zerolog.ParseLevel(levelString)
if err != nil {
return err
}
SetGlobalLevel(level)
return nil
}
// GetGlobalLogger returns the global logger.
func GetGlobalLogger() *Logger {
return global
}
// WithComponent enables the global logger to chain loggers with additional context, component name.
func WithComponent(component string) *Logger {
return global.WithComponent(component)
}
// WithComponent enables the current logger to chain loggers with additional context, component name.
func (lg *Logger) WithComponent(component string) *Logger {
zerolog := lg.logger.With().Str(componentKey, component).Logger()
return &Logger{
logger: &zerolog,
}
}
// WithInterface adds an interface to the logger context.
func (lg *Logger) WithInterface(key string, value interface{}) *Logger {
zerolog := lg.logger.With().Interface(key, value).Logger()
return &Logger{
logger: &zerolog,
}
}
// WithInterface adds an interface to the global logger context.
func WithInterface(key string, value interface{}) *Logger {
return global.WithInterface(key, value)
}
// WithStr adds a string to the logger context.
func (lg *Logger) WithStr(key string, value string) *Logger {
zerolog := lg.logger.With().Str(key, value).Logger()
return &Logger{
logger: &zerolog,
}
}
// WithStr adds a string to the global logger context.
func WithStr(key string, value string) *Logger {
return global.WithStr(key, value)
}
// WithBool adds a bool to the logger context.
func (lg *Logger) WithBool(key string, value bool) *Logger {
zerolog := lg.logger.With().Bool(key, value).Logger()
return &Logger{
logger: &zerolog,
}
}
// WithBool adds a bool to the global logger context.
func WithBool(key string, value bool) *Logger {
return global.WithBool(key, value)
}
// GetZerolog returns underlying zerolog logger.
func (lg *Logger) GetZerolog() *zerolog.Logger {
return lg.logger
}
// NewFromZerolog creates the logger from zerolog instance.
func (lg *Logger) NewFromZerolog(logger *zerolog.Logger) *Logger {
return &Logger{
logger: logger,
}
}
// Output duplicates the current logger and sets w as its output.
func (lg *Logger) Output(w io.Writer) *Logger {
zerolog := lg.logger.Output(w)
return &Logger{
logger: &zerolog,
}
}
// Output duplicates the global logger and sets w as its output.
func Output(w io.Writer) *Logger {
return global.Output(w)
}
// With creates a child logger of the current logger with the field added to its context.
func (lg *Logger) With() zerolog.Context {
return lg.logger.With()
}
// With creates a child logger of the global logger with the field added to its context.
func With() zerolog.Context {
return global.With()
}
// Level creates a child logger of the current logger with the minimum accepted level set to level.
func (lg *Logger) Level(level zerolog.Level) *Logger {
zerolog := lg.logger.Level(level)
return &Logger{
logger: &zerolog,
}
}
// Level creates a child logger of the global logger with the minimum accepted level set to level.
func Level(level zerolog.Level) *Logger {
return global.Level(level)
}
// GetLevel returns the current logger level.
func (lg *Logger) GetLevel() zerolog.Level {
return lg.logger.GetLevel()
}
// GetLevel returns the global logger level.
func GetLevel() zerolog.Level {
return global.GetLevel()
}
// Sample returns the current logger with the s sampler.
func (lg *Logger) Sample(sampler zerolog.Sampler) *Logger {
zerolog := lg.WithBool(sampledKey, true).logger.Sample(sampler)
return &Logger{
logger: &zerolog,
}
}
// Sample returns the global logger with the s sampler.
func Sample(sampler zerolog.Sampler) *Logger {
return global.Sample(sampler)
}
// Hook returns the current logger with the h hook.
func (lg *Logger) Hook(hook zerolog.Hook) *Logger {
zerolog := lg.logger.Hook(hook)
return &Logger{
logger: &zerolog,
}
}
// Hook returns the global logger with the h hook.
func Hook(hook zerolog.Hook) *Logger {
return global.Hook(hook)
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Trace() *zerolog.Event {
return lg.logger.Trace()
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func Trace() *zerolog.Event {
return global.Trace()
}
// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Debug() *zerolog.Event {
return lg.logger.Debug()
}
// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func Debug() *zerolog.Event {
return global.Debug()
}
// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Info() *zerolog.Event {
return lg.logger.Info()
}
// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func Info() *zerolog.Event {
return global.Info()
}
// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Warn() *zerolog.Event {
return lg.logger.Warn()
}
// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func Warn() *zerolog.Event {
return global.Warn()
}
// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Error() *zerolog.Event {
return lg.logger.Error()
}
// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func Error() *zerolog.Event {
return global.Error()
}
// Fatal starts a new message with fatal level. This is an alias for Panic.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Fatal() *zerolog.Event {
return lg.logger.Panic()
}
// Fatal starts a new message with fatal level.
//
// You must call Msg on the returned event in order to send the event.
func Fatal() *zerolog.Event {
return global.Fatal()
}
// Panic starts a new message with panic level. The panic() function
// is called by the Msg method, which stops the ordinary flow of a goroutine and
// invokes any registered PanicHandler.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Panic() *zerolog.Event {
return lg.logger.Panic()
}
// Panic starts a new message with panic level. The message is also sent
// to the panic function.
//
// You must call Msg on the returned event in order to send the event.
func Panic() *zerolog.Event {
return global.Panic()
}
// WithLevel starts a new message with level. Unlike Fatal and Panic
// methods, WithLevel does not terminate the program or stop the ordinary
// flow of a goroutine when used with their respective levels.
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) WithLevel(level zerolog.Level) *zerolog.Event {
return lg.logger.WithLevel(level)
}
// WithLevel starts a new message with level.
//
// You must call Msg on the returned event in order to send the event.
func WithLevel(level zerolog.Level) *zerolog.Event {
return global.WithLevel(level)
}
// Log starts a new message with no level. This is equivalent to using lg.WithLevel(NoLevel).
//
// You must call Msg on the returned event in order to send the event.
func (lg *Logger) Log() *zerolog.Event {
return lg.logger.Log()
}
// Log starts a new message with no level. This is equivalent to using WithLevel(NoLevel).
//
// You must call Msg on the returned event in order to send the event.
func Log() *zerolog.Event {
return global.Log()
}
func printfEvent(e *zerolog.Event, format string, v ...interface{}) {
if e.Enabled() {
e.CallerSkipFrame(2).Msg(fmt.Sprintf(format, v...))
}
}
func printEvent(e *zerolog.Event, v ...interface{}) {
if e.Enabled() {
e.CallerSkipFrame(2).Msg(fmt.Sprint(v...))
}
}
func printlnEvent(e *zerolog.Event, v ...interface{}) {
if e.Enabled() {
e.CallerSkipFrame(2).Msg(fmt.Sprintln(v...))
}
}
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func (lg *Logger) Print(v ...interface{}) {
printEvent(lg.logger.Debug(), v...)
}
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
printEvent(global.logger.Debug(), v...)
}
// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Printf(format string, v ...interface{}) {
printfEvent(lg.logger.Debug(), format, v...)
}
// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
printfEvent(global.logger.Debug(), format, v...)
}
// Println sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (lg *Logger) Println(v ...interface{}) {
printlnEvent(lg.logger.Debug(), v...)
}
// Println sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func Println(v ...interface{}) {
printlnEvent(global.logger.Debug(), v...)
}
// more adapters
/* Fatal */
// Fatalf sends a log event using fatal level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Fatalf(format string, v ...interface{}) {
printfEvent(lg.logger.Fatal(), format, v...)
}
// Fatalf sends a log event using fatal level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Fatalf(format string, v ...interface{}) {
printfEvent(global.logger.Fatal(), format, v...)
}
// Fatalln sends a log event using fatal level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (lg *Logger) Fatalln(v ...interface{}) {
printlnEvent(lg.logger.Fatal(), v...)
}
// Fatalln sends a log event using fatal level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func Fatalln(v ...interface{}) {
printlnEvent(global.logger.Fatal(), v...)
}
/* Panic */
// Panicf sends a log event using panic level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Panicf(format string, v ...interface{}) {
printfEvent(lg.logger.Panic(), format, v...)
}
// Panicf sends a log event using panic level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Panicf(format string, v ...interface{}) {
printfEvent(global.logger.Panic(), format, v...)
}
// Panicln sends a log event using panic level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (lg *Logger) Panicln(v ...interface{}) {
printlnEvent(lg.logger.Panic(), v...)
}
// Panicln sends a log event using panic level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func Panicln(v ...interface{}) {
printlnEvent(global.logger.Panic(), v...)
}
/* Debug */
// Debugf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Debugf(format string, v ...interface{}) {
printfEvent(lg.logger.Debug(), format, v...)
}
// Debugf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Debugf(format string, v ...interface{}) {
printfEvent(global.logger.Debug(), format, v...)
}
// Debugln sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (lg *Logger) Debugln(v ...interface{}) {
printlnEvent(lg.logger.Debug(), v...)
}
// Debugln sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func Debugln(v ...interface{}) {
printlnEvent(global.logger.Debug(), v...)
}
/* Info */
// Infof sends a log event using info level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Infof(format string, v ...interface{}) {
printfEvent(lg.logger.Info(), format, v...)
}
// Infof sends a log event using info level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Infof(format string, v ...interface{}) {
printfEvent(global.logger.Info(), format, v...)
}
// Infoln sends a log event using info level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (lg *Logger) Infoln(v ...interface{}) {
printlnEvent(lg.logger.Info(), v...)
}
// Infoln sends a log event using info level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func Infoln(v ...interface{}) {
printlnEvent(global.logger.Info(), v...)
}
/* Warn */
// Warnf sends a log event using warn level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Warnf(format string, v ...interface{}) {
printfEvent(lg.logger.Warn(), format, v...)
}
// Warnf sends a log event using warn level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Warnf(format string, v ...interface{}) {
printfEvent(global.logger.Warn(), format, v...)
}
// Warnln sends a log event using warn level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (lg *Logger) Warnln(v ...interface{}) {
printlnEvent(lg.logger.Warn(), v...)
}
// Warnln sends a log event using warn level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func Warnln(v ...interface{}) {
printlnEvent(global.logger.Warn(), v...)
}
/* Error */
// Errorf sends a log event using error level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (lg *Logger) Errorf(format string, v ...interface{}) {
printfEvent(lg.logger.Error(), format, v...)
}
// Errorf sends a log event using error level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, v ...interface{}) {
printfEvent(global.logger.Error(), format, v...)
}
// Errorln sends a log event using error level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (lg *Logger) Errorln(v ...interface{}) {
printlnEvent(lg.logger.Error(), v...)
}
// Errorln sends a log event using error level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func Errorln(v ...interface{}) {
printlnEvent(global.logger.Error(), v...)
}
// Write implements io.Writer interface.
func (lg *Logger) Write(p []byte) (n int, err error) {
return lg.logger.Write(p)
}
// Write implements io.Writer interface.
func Write(p []byte) (n int, err error) {
return global.logger.Write(p)
}