-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.go
149 lines (117 loc) · 3.35 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
package log
import (
"context"
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Fields = zapcore.Field
type Logger interface {
WithField(key string, value interface{}) Logger
WithFields(fields Fields) Logger
WithError(err error) Logger
WithTracing(ctx context.Context) Logger
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Warningf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Panicf(format string, args ...interface{})
Debug(args ...interface{})
Info(args ...interface{})
Warn(args ...interface{})
Warning(args ...interface{})
Error(args ...interface{})
Fatal(args ...interface{})
Panic(args ...interface{})
Sync() error
}
var origLogger *zap.SugaredLogger
var baseLogger logger
func init() {
encoderConf := zap.NewProductionEncoderConfig()
// Set RFC3339 timestamp encoding format
encoderConf.TimeKey = "timestamp"
encoderConf.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(time.RFC3339))
}
encoderConf.CallerKey = "source"
l := zap.New(
zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConf),
zapcore.Lock(os.Stdout),
zap.NewAtomicLevel(),
),
zap.AddCaller(),
zap.AddCallerSkip(1),
zap.AddStacktrace(zapcore.InfoLevel),
)
origLogger = l.Sugar()
baseLogger = logger{origLogger}
}
func Base() Logger {
return baseLogger
}
func WithField(key string, value interface{}) Logger {
return baseLogger.WithField(key, value)
}
func WithFields(fields Fields) Logger {
return baseLogger.WithFields(fields)
}
func WithError(err error) Logger {
return baseLogger.WithError(err)
}
// WithTracing will take an OpenCensus trace and add log fields for Datadog.
func WithTracing(ctx context.Context) Logger {
return baseLogger.WithTracing(ctx)
}
// We must directly call the bundled logger here (whenever a func instead of
// method is used), reason is for the "caller skip" calculation to be correct
// in all instances.
// When we are called as a function `log.Info("msg")` vs method
// `log.WithField("key", "val").Info("msg")` we would otherwise end up with
// 3 vs 2 stack entries.
func Debugf(format string, args ...interface{}) {
baseLogger.logger.Debugf(format, args...)
}
func Infof(format string, args ...interface{}) {
baseLogger.logger.Infof(format, args...)
}
func Warnf(format string, args ...interface{}) {
baseLogger.logger.Warnf(format, args...)
}
func Warningf(format string, args ...interface{}) {
baseLogger.logger.Warnf(format, args...)
}
func Errorf(format string, args ...interface{}) {
baseLogger.logger.Errorf(format, args...)
}
func Fatalf(format string, args ...interface{}) {
baseLogger.logger.Fatalf(format, args...)
}
func Panicf(format string, args ...interface{}) {
baseLogger.logger.Panicf(format, args...)
}
func Debug(args ...interface{}) {
baseLogger.logger.Debug(args...)
}
func Info(args ...interface{}) {
baseLogger.logger.Info(args...)
}
func Warn(args ...interface{}) {
baseLogger.logger.Warn(args...)
}
func Warning(args ...interface{}) {
baseLogger.logger.Warn(args...)
}
func Error(args ...interface{}) {
baseLogger.logger.Error(args...)
}
func Fatal(args ...interface{}) {
baseLogger.logger.Fatal(args...)
}
func Panic(args ...interface{}) {
baseLogger.logger.Panic(args...)
}