-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
76 lines (66 loc) · 1.67 KB
/
logging.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 main
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger A simple interface to abstract away any 3rd party logging module used. Only
// the required functionality is exposed.
type Logger interface {
Debug(args ...interface{})
Warn(args ...interface{})
Info(args ...interface{})
Panic(args ...interface{})
Sync()
}
// simpleLogger wraps the 3rd party zap logger module
type simpleLogger struct {
l *zap.SugaredLogger
}
func NewLogger(path string) Logger {
loggerConfig := zap.Config{
Level: zap.NewAtomicLevelAt(zap.InfoLevel),
Development: false,
Encoding: "console",
DisableStacktrace: true,
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
FunctionKey: zapcore.OmitKey,
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr", path},
ErrorOutputPaths: []string{"stderr", path},
}
logger, err := loggerConfig.Build()
if err != nil {
panic(err)
}
sugar := logger.Sugar()
return &simpleLogger{l: sugar}
}
func (g simpleLogger) Warn(args ...interface{}) {
g.l.Warn(args)
}
func (g simpleLogger) Info(args ...interface{}) {
g.l.Info(args)
}
func (g simpleLogger) Debug(args ...interface{}) {
g.l.Debug(args)
}
func (g simpleLogger) Panic(args ...interface{}) {
g.l.Panic(args)
}
func (g simpleLogger) Sync() {
err := g.l.Sync()
if err != nil {
return
}
}