-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
76 lines (60 loc) · 1.82 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
package goraft
import (
"io"
"time"
"go.uber.org/zap/zapcore"
"go.uber.org/zap"
)
type (
// Logger defines a logging interface a Raft node expects to use.
Logger interface {
Info(msg string, args ...interface{})
Debug(msg string, args ...interface{})
Error(msg string, args ...interface{})
With(args ...interface{}) Logger
}
// raftLogger defines a default Raft logger using zap as the underlying
// logging library.
raftLogger struct {
zapLogger *zap.SugaredLogger
}
)
func NewRaftLogger(w io.Writer) Logger {
// create a zap WriteSyncer based on the provided io.Writer
ws := zapcore.AddSync(w)
cfg := zap.NewProductionConfig()
// create a zap encoder config and override time key and encoder
encCfg := zap.NewProductionEncoderConfig()
encCfg.TimeKey = "time"
encCfg.EncodeTime = rfc3339TimeEncoder
enc := zapcore.NewJSONEncoder(encCfg)
core := zapcore.NewCore(enc, ws, cfg.Level)
// set logging error output and sampling options (based on zap production)
opts := []zap.Option{
zap.ErrorOutput(ws),
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSampler(
core, time.Second, cfg.Sampling.Initial, cfg.Sampling.Thereafter,
)
}),
}
rl := &raftLogger{zap.New(core, opts...).Sugar()}
defer rl.zapLogger.Sync() // nolint: errcheck
return rl
}
func (rl *raftLogger) Info(msg string, args ...interface{}) {
rl.zapLogger.Infow(msg, args...)
}
func (rl *raftLogger) Debug(msg string, args ...interface{}) {
rl.zapLogger.Debugw(msg, args...)
}
func (rl *raftLogger) Error(msg string, args ...interface{}) {
rl.zapLogger.Errorw(msg, args...)
}
func (rl *raftLogger) With(args ...interface{}) Logger {
rl.zapLogger = rl.zapLogger.With(args)
return rl
}
func rfc3339TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.UTC().Format(time.RFC3339))
}