-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
311 lines (248 loc) · 7.39 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Package log provides a global logger for zerolog.
package log
import (
"context"
"fmt"
"io"
stdlog "log"
"os"
"time"
"github.com/getsentry/sentry-go"
"github.com/rs/zerolog"
)
const (
defaultLevel = "debug"
ctxRequestIDKey = "request_id"
defaultCallerSkipFrames = 2
standAloneCallerSkipFrames = 6
)
type ZLogger struct {
zerolog.Logger
cfg Config
}
// logger is the global logger.
var logger, _ = newZerolog(
Config{
Level: defaultLevel,
Format: FormatText,
Caller: &CallerConfig{CallerSkipFrames: standAloneCallerSkipFrames},
},
os.Stdout,
)
// set global Zerolog logger.
func Init(stage string, cfg Config, serviceAlias string, serviceVersion string, w io.Writer) (err error) {
if w == nil {
w = os.Stdout
}
if cfg.Format == "" {
cfg.Format = FormatText
}
if cfg.Caller == nil {
cfg.Caller = &CallerConfig{
CallerSkipFrames: defaultCallerSkipFrames,
}
}
if cfg.Sentry == nil || !cfg.Sentry.Enable || cfg.Sentry.DSN == "" {
logger, err = newZerolog(cfg, w)
if err != nil {
return fmt.Errorf("logger init newZerolog error: %w", err)
}
return nil
}
sentrySyncTransport := sentry.NewHTTPSyncTransport()
sentrySyncTransport.Timeout = time.Second * 2 //nolint:gomnd // 2 second transport timeout
client, err := sentry.NewClient(sentry.ClientOptions{
Dsn: cfg.Sentry.DSN,
DebugWriter: os.Stderr,
Debug: cfg.Sentry.Debug,
ServerName: serviceAlias,
Release: serviceVersion,
Environment: stage,
Transport: sentrySyncTransport,
})
if err != nil {
return fmt.Errorf("logger init raven.New error: %w", err)
}
h := sentry.NewHub(client, sentry.NewScope())
pr, pw := io.Pipe()
go sentryPush(h, pr)
cfg.Format = FormatJSON
logger, err = newZerolog(cfg, io.MultiWriter(w, pw))
return err
}
func newZerolog(cfg Config, w io.Writer) (logger ZLogger, err error) {
// UNIX Time is faster and smaller than most timestamps
// If you set zerolog.TimeFieldFormat to an empty string,
// logs will write with UNIX time
zerolog.TimeFieldFormat = time.RFC3339Nano
// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
zerolog.CallerSkipFrameCount = cfg.Caller.CallerSkipFrames
output := w
if cfg.Format == "text" {
// pretty print during development
out := zerolog.ConsoleWriter{Out: w, TimeFormat: zerolog.TimeFieldFormat, NoColor: cfg.NoColor}
out.PartsOrder = []string{
zerolog.TimestampFieldName,
zerolog.LevelFieldName,
zerolog.MessageFieldName,
}
if !cfg.Caller.Disabled {
out.PartsOrder = append(out.PartsOrder, zerolog.CallerFieldName)
}
out.FormatMessage = func(i interface{}) string {
if i == nil {
return ""
}
return fmt.Sprintf("|> %s <|", i)
}
output = out
}
level, err := getLevel(cfg.Level)
if err != nil {
return logger, err
}
lctx := zerolog.New(output).With().Timestamp()
if !cfg.Caller.Disabled {
lctx = lctx.Caller()
}
logger.Logger = lctx.Logger().Level(level)
logger.cfg = cfg
stdlog.SetFlags(0)
stdlog.SetOutput(logger)
return logger, nil
}
func getLevel(lvl string) (zerolog.Level, error) {
if lvl == "" {
lvl = defaultLevel
}
level, err := zerolog.ParseLevel(lvl)
if err != nil {
return zerolog.DebugLevel, fmt.Errorf("get level error: %w", err)
}
return level, nil
}
func Logger() ZLogger {
return logger
}
// Output duplicates the global logger and sets w as its output.
func Output(w io.Writer) zerolog.Logger {
l, _ := newZerolog(logger.cfg, w)
return l.Logger
}
// With creates a child logger with the field added to its context.
func With() zerolog.Context {
return logger.With()
}
// Level creates a child logger with the minimum accepted level set to level.
func Level(level zerolog.Level) zerolog.Logger {
return logger.Level(level)
}
// LevelString creates a child logger with the minimum accepted level set to level passed as string.
func LevelString(lvl string) zerolog.Logger {
level, _ := getLevel(lvl)
return logger.Level(level)
}
// Sample returns a logger with the s sampler.
func Sample(s zerolog.Sampler) zerolog.Logger {
return logger.Sample(s)
}
// Hook returns a logger with the h Hook.
func Hook(h zerolog.Hook) zerolog.Logger {
return logger.Hook(h)
}
// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func Err(err error) *zerolog.Event {
return logger.Err(err)
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func Trace() *zerolog.Event {
return logger.Trace()
}
// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func Debug() *zerolog.Event {
return logger.Debug()
}
// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func Info() *zerolog.Event {
return logger.Info()
}
// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func Warn() *zerolog.Event {
return logger.Warn()
}
// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func Error() *zerolog.Event {
return logger.Error()
}
// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method.
//
// You must call Msg on the returned event in order to send the event.
func Fatal() *zerolog.Event {
return logger.Fatal()
}
// Panic starts a new message with panic level. The message is also sent
// to the panic function.
//
// You must call Msg on the returned event in order to send the event.
func Panic() *zerolog.Event {
return logger.Panic()
}
// WithLevel starts a new message with level.
//
// You must call Msg on the returned event in order to send the event.
func WithLevel(level zerolog.Level) *zerolog.Event {
return logger.WithLevel(level)
}
// Log starts a new message with no level. Setting zerolog.GlobalLevel to
// zerolog.Disabled will still disable events produced by this method.
//
// You must call Msg on the returned event in order to send the event.
func Log() *zerolog.Event {
return logger.Log()
}
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
logger.Print(v...)
}
// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
logger.Printf(format, v...)
}
// Ctx returns the logger associated with the ctx. If no logger
// is associated, a disabled logger is returned.
func Ctx(ctx context.Context) *zerolog.Logger {
return zerolog.Ctx(ctx)
}
func contextFields(ctx context.Context) (fields map[string]interface{}) {
fields = make(map[string]interface{})
if requestID, ok := ctx.Value(ctxRequestIDKey).(fmt.Stringer); ok && requestID.String() != "00000000-0000-0000-0000-000000000000" {
fields[ctxRequestIDKey] = requestID.String()
}
if requestID, ok := ctx.Value(ctxRequestIDKey).(string); ok && requestID != "00000000-0000-0000-0000-000000000000" {
fields[ctxRequestIDKey] = requestID
}
return fields
}
// With creates a child logger with the field added to its context.
func WithCtx(ctx context.Context) *zerolog.Logger {
l := With()
fields := contextFields(ctx)
l2 := l.Fields(fields).Logger()
return &l2
}