-
Notifications
You must be signed in to change notification settings - Fork 4
/
presets.go
55 lines (46 loc) · 1.37 KB
/
presets.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
package golog
import (
"bufio"
"context"
"os"
"time"
)
// NewProductionLogger returns a pre-configured logger for production environment
func NewProductionLogger(lvl Level) (StdLogger, Flusher) {
return NewWithEncoder(lvl, NewJsonEncoder(DefaultJsonConfig()))
}
// NewDevelopmentLogger returns a pre-configured logger for development environment
func NewDevelopmentLogger(lvl Level) (StdLogger, Flusher) {
textConfig := DefaultTextConfig()
textConfig.LevelFormatter = ColoredLevelFormatter()
return NewWithEncoder(lvl, NewTextEncoder(textConfig))
}
// NewWithEncoder returns a preset logger with a customer Encoder
func NewWithEncoder(lvl Level, enc Encoder) (StdLogger, Flusher) {
w := NewBufWriter(
enc,
bufio.NewWriter(os.Stdout),
DefaultErrorHandler(),
lvl,
)
return newLogger(lvl, w)
}
func newLogger(lvl Level, wf Writer) (StdLogger, Flusher) {
log := New(
wf,
NewLevelCheckerOption(lvl),
NewTimestampDecoratorOption("timestamp", time.RFC3339Nano),
NewStackTraceDecoratorOption("stacktrace", 5),
)
// force flushing data to the disk every 5 seconds
go func() {
flusher := NewTickFlusher(wf, 5*time.Second)
if err := flusher.Flush(); err != nil {
log.With(Err(err)).Warn(context.Background(), "entries were not flushed")
}
}()
// Set the created logger as main one for the golog package as well
SetLogger(log)
SetCheckLogger(log)
return log, wf
}