-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
296 lines (246 loc) · 9.31 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
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
package log
import (
"context"
"errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/SyntSugar/ss-infra-go/consts"
prome "github.com/SyntSugar/ss-infra-go/prometheus"
)
const (
namespace = "infra"
subsystem = "zap"
ctxLogger consts.ContextKey = "ctx_logger"
)
var (
globalLogger *Logger
globalMetric metric
)
var ErrNil = errors.New("the value is null")
func NewLogger(loglevel, encoding string, samplingConfig *zap.SamplingConfig) (*Logger, error) {
zapLogger, err := newZap(loglevel, encoding, samplingConfig)
if err != nil {
return nil, err
}
debugger, err := newZap(zap.DebugLevel.String(), encoding, samplingConfig)
if err != nil {
return nil, err
}
logger := &Logger{
logger: zapLogger,
debugger: debugger,
}
logger = logger.WithOptions(zap.AddCallerSkip(1))
return logger, nil
}
// DefaultSamplingConfig return the default sampling config for the zap logger.
// it would start sampling after exceeding initial entries(default was 64) per second
// and only sample the Thereafter(default was 10 which means sampling 10%) th entry.
func DefaultSamplingConfig() *zap.SamplingConfig {
return &zap.SamplingConfig{
Initial: 64, // log 64 entries per second before sampling
Thereafter: 10, // log at 10th entry after exceeding the 64 entries
Hook: logSamplingMetrics,
}
}
func GlobalLogger() *Logger {
return globalLogger
}
type Logger struct {
logger *zap.Logger
debugger *zap.Logger
}
// Named adds a new path segment to the logger's name. Segments are joined by
// periods. By default, Loggers are unnamed.
func (l *Logger) Named(s string) *Logger {
if s == "" {
return l
}
l.logger = l.logger.Named(s)
l.debugger = l.debugger.Named(s)
return l
}
// WithOptions clones the current Logger, applies the supplied Options, and
// returns the resulting Logger. It's safe to use concurrently.
func (l *Logger) WithOptions(opts ...zap.Option) *Logger {
copy := l.clone()
copy.logger = copy.logger.WithOptions(opts...)
copy.debugger = copy.debugger.WithOptions(opts...)
return copy
}
// With creates a child logger and adds structured context to it. Fields added
// to the child don't affect the parent, and vice versa.
func (l *Logger) With(fields ...zap.Field) *Logger {
if len(fields) == 0 {
return l
}
copy := l.clone()
copy.logger = copy.logger.With(fields...)
copy.debugger = copy.debugger.With(fields...)
return copy
}
// Check returns a CheckedEntry if logging a message at the specified level
// is enabled. It's a completely optional optimization; in high-performance
// applications, Check can help avoid allocating a slice to hold fields.
func (l *Logger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
return l.logger.Check(lvl, msg)
}
// GetZapLogger would return the zap logger
func (l *Logger) GetZapLogger() *zap.Logger {
return l.logger
}
// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) Debug(msg string, fields ...zap.Field) {
l.logger.Debug(msg, fields...)
}
// DebugCtx logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) DebugCtx(ctx context.Context, msg string, fields ...zap.Field) {
logger := l.logger
if l.IsDynamicDebugEnabled(ctx) {
logger = l.debugger
}
logger.Debug(msg, append(fields, GetContextFields(ctx)...)...)
}
// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) Info(msg string, fields ...zap.Field) {
l.logger.Info(msg, fields...)
}
// InfoCtx logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) InfoCtx(ctx context.Context, msg string, fields ...zap.Field) {
fields = append(fields, GetContextFields(ctx)...)
l.logger.Info(msg, fields...)
}
// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) Warn(msg string, fields ...zap.Field) {
l.logger.Warn(msg, fields...)
}
// WarnCtx logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) WarnCtx(ctx context.Context, msg string, fields ...zap.Field) {
fields = append(fields, GetContextFields(ctx)...)
l.logger.Warn(msg, fields...)
}
// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) Error(msg string, fields ...zap.Field) {
l.logger.Error(msg, fields...)
}
// ErrorCtx logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l *Logger) ErrorCtx(ctx context.Context, msg string, fields ...zap.Field) {
fields = append(fields, GetContextFields(ctx)...)
l.logger.Error(msg, fields...)
}
// DPanic logs a message at DPanicLevel. The message includes any fields
// passed at the log site, as well as any fields accumulated on the logger.
//
// If the logger is in development mode, it then panics (DPanic means
// "development panic"). This is useful for catching errors that are
// recoverable, but shouldn't ever happen.
func (l *Logger) DPanic(msg string, fields ...zap.Field) {
l.logger.DPanic(msg, fields...)
}
// DPanicCtx logs a message at DPanicLevel. The message includes any fields
// passed at the log site, as well as any fields accumulated on the logger.
//
// If the logger is in development mode, it then panics (DPanic means
// "development panic"). This is useful for catching errors that are
// recoverable, but shouldn't ever happen.
func (l *Logger) DPanicCtx(ctx context.Context, msg string, fields ...zap.Field) {
fields = append(fields, GetContextFields(ctx)...)
l.logger.DPanic(msg, fields...)
}
// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func (l *Logger) Panic(msg string, fields ...zap.Field) {
l.logger.DPanic(msg, fields...)
}
// PanicCtx logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func (l *Logger) PanicCtx(ctx context.Context, msg string, fields ...zap.Field) {
fields = append(fields, GetContextFields(ctx)...)
l.logger.DPanic(msg, fields...)
}
// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func (l *Logger) Fatal(msg string, fields ...zap.Field) {
l.logger.DPanic(msg, fields...)
}
// FatalCtx logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func (l *Logger) FatalCtx(ctx context.Context, msg string, fields ...zap.Field) {
fields = append(fields, GetContextFields(ctx)...)
l.logger.DPanic(msg, fields...)
}
// Sync calls the underlying Core's Sync method, flushing any buffered log
// entries. Applications should take care to call Sync before exiting.
func (l *Logger) Sync() error {
return l.logger.Sync()
}
// Core returns the Logger's underlying zapcore.Core.
func (l *Logger) Core() zapcore.Core {
return l.logger.Core()
}
func (l *Logger) clone() *Logger {
copy := *l
return ©
}
// IsDynamicDebugEnabled check whether debug level logging enabled.
func (l *Logger) IsDynamicDebugEnabled(ctx context.Context) bool {
if ctx == nil || l.Core().Enabled(zap.DebugLevel) {
return false
}
enabled, ok := ctx.Value(consts.ContextKeyEnableDebugLogging).(bool)
return ok && enabled
}
func init() {
var err error
globalLogger, err = NewLogger("info", "json", DefaultSamplingConfig())
if err != nil {
panic("Failed to create global logger" + err.Error())
}
labels := []string{"level", "decision"}
globalMetric.samplingCounter = prome.NewCounterHelper(namespace, subsystem, "sampling", labels...)
}
func newZap(loglevel, encoding string, samplingConfig *zap.SamplingConfig) (*zap.Logger, error) {
// Set default encoding if empty
if encoding == "" {
encoding = "json"
}
// Use default sampling config if not provided
if samplingConfig == nil {
samplingConfig = DefaultSamplingConfig()
}
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "event_time"
encoderConfig.LevelKey = "severity"
encoderConfig.MessageKey = "message"
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg := zap.NewProductionConfig()
cfg.OutputPaths = []string{"stdout"}
cfg.EncoderConfig = encoderConfig
cfg.Encoding = encoding
cfg.Sampling = samplingConfig
var level zapcore.Level
if err := level.Set(loglevel); err != nil {
return nil, err
}
cfg.Level = zap.NewAtomicLevelAt(level)
return cfg.Build(zap.AddStacktrace(zapcore.DPanicLevel), zap.AddCallerSkip(0))
}