-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
217 lines (192 loc) · 6.43 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
package logger
import (
"context"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"net/http"
)
// CSugaredLogger is a superset of zap.SugaredLogger
type CSugaredLogger struct {
zap.SugaredLogger
}
// CLogger is a superset of zap.Logger
type CLogger struct {
zap.Logger
}
var logger *CLogger
var correlationIdContextKey string
var correlationIdFieldKey string
// WithContextCorrelationId returns an instance of the same logger with the correlation ID taken from the context added to it.
func (l *CLogger) WithContextCorrelationId(ctx context.Context) *CLogger {
correlationId := ctx.Value(correlationIdContextKey)
return l.WithCorrelationId(correlationId)
}
// WithContextCorrelationId returns an instance of the same logger with the correlation ID taken from the context added to it.
func (l *CSugaredLogger) WithContextCorrelationId(ctx context.Context) *CSugaredLogger {
correlationId := ctx.Value(correlationIdContextKey)
return l.WithCorrelationId(correlationId)
}
// WithCorrelationId returns an instance of the same logger with the correlation ID field added to it.
func (l *CLogger) WithCorrelationId(correlationId interface{}) *CLogger {
if s, ok := correlationId.(string); ok {
return &CLogger{*l.Logger.With(zap.Stringp(correlationIdFieldKey, &s))}
}
return l
}
// WithCorrelationId returns an instance of the same logger with the correlation ID field added to it.
func (l *CSugaredLogger) WithCorrelationId(correlationId interface{}) *CSugaredLogger {
if s, ok := correlationId.(string); ok {
return &CSugaredLogger{*l.SugaredLogger.With(zap.Stringp(correlationIdFieldKey, &s))}
}
return l
}
func (l *CLogger) With(args ...zap.Field) *CLogger {
return &CLogger{*l.Logger.With(args...)}
}
func (l *CSugaredLogger) With(args ...interface{}) *CSugaredLogger {
return &CSugaredLogger{*l.SugaredLogger.With(args...)}
}
// SugaredLogger returns an instance of the sugared logger. You must have initialized the logger prior to this call.
func SugaredLogger() *CSugaredLogger {
if logger == nil {
panic("logger not initialized. Call Init(ctx)")
}
l := logger.Sugar()
return &CSugaredLogger{*l}
}
// Logger returns an instance of the sugar-free logger. You must have initialized the logger prior to this call.
func Logger() *CLogger {
if logger == nil {
panic("logger not initialized. Call Init(ctx)")
}
return logger
}
// SetCorrelationIdFieldKey sets the correlation ID field key in JSON responses. By default, it is "correlation_id"
func SetCorrelationIdFieldKey(key string) {
if key == "" {
return
}
correlationIdFieldKey = key
}
// SetCorrelationIdContextKey sets the correlation ID context key. By default, it is "correlation_id"
func SetCorrelationIdContextKey(key string) {
if key == "" {
return
}
correlationIdContextKey = key
}
// Init bootstraps the logger. You must call this method just once at the
// beginning of your application. The default log level is Info.
//
// Call Logger() or SugaredLogger() to retrieve an instance of the desired type
// of logger. Use WithCorrelationId() or WithContextCorrelationId() to add a
// correlation ID to your logs.
//
// If enableLogLevelEndpoint is true, then an HTTP endpoint on port 53835 at
// /logger is exposed which can be used to change the log level dynamically. See
// the Zap documentation for more information.
//
// If developmentMode is true, then the logLevel is set to Debug and caller
// fields are more explicit. Do not enable this in production.
func Init(ctx context.Context, enableLogLevelEndpoint, developmentMode bool) {
if logger != nil {
return
}
var (
zapConfig zap.Config
encoderConfig zapcore.EncoderConfig
atom zap.AtomicLevel
loggerMode []string
)
correlationIdContextKey = "correlation_id"
correlationIdFieldKey = "correlation_id"
if developmentMode {
loggerMode = append(loggerMode, "dev")
atom = zap.NewAtomicLevelAt(zap.DebugLevel)
encoderConfig = zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: "func",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.MillisDurationEncoder,
EncodeCaller: zapcore.FullCallerEncoder,
}
zapConfig = zap.Config{
Level: atom,
Development: true,
DisableCaller: false,
DisableStacktrace: false,
Sampling: nil,
Encoding: "json",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
InitialFields: nil,
}
} else {
loggerMode = append(loggerMode, "prod")
atom = zap.NewAtomicLevelAt(zap.InfoLevel)
encoderConfig = zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.MillisDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
zapConfig = zap.Config{
Level: atom,
Development: false,
DisableCaller: false,
DisableStacktrace: false,
Sampling: &zap.SamplingConfig{Initial: 100, Thereafter: 100},
Encoding: "json",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
InitialFields: nil,
}
}
if enableLogLevelEndpoint {
loggerMode = append(loggerMode, "serveHttp")
go func() {
mux := http.NewServeMux()
mux.Handle("/loglevel", atom)
_ = http.ListenAndServe(":53835", mux)
}()
}
l, err := zapConfig.Build()
if err != nil {
panic(fmt.Sprintf("logger initalization error: %s", err.Error()))
}
l.Info("Logger initialized successfully", zap.Strings("logger_modes", loggerMode))
if enableLogLevelEndpoint {
l.Info("Logger HTTP Server active on :53835/loglevel")
}
logger = &CLogger{*l}
}
func (l *CSugaredLogger) Print(args ...interface{}) {
l.Debug(args...)
}
func (l *CSugaredLogger) Println(args ...interface{}) {
l.Debug(args...)
}
func (l *CSugaredLogger) Printf(format string, args ...interface{}) {
l.Debugf(format, args...)
}
func (l *CSugaredLogger) Fatalln(args ...interface{}) {
l.Fatal(args)
}