-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger.go
More file actions
523 lines (433 loc) · 14 KB
/
Copy pathlogger.go
File metadata and controls
523 lines (433 loc) · 14 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package logger
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"github.com/pkg/errors"
)
// Logger allows you to control logging with message levels and subsystem controls.
// Use the "Include" flags in the Format field to specify which fields should be included in each
// log message.
// Subsystem log entries can be enabled per subsystem.
// For example the parent package can specify if they want to see logs from a subsystem and how
// they want to see them.
//
// Sample Setup:
// // Create a log config and set it up.
// logConfig := logger.NewDevelopmentConfig()
// // Log to stderr (default) and main.log.
// // To only log to main.log call SetFile instead of AddFile.
// logConfig.Main.AddFile("./tmp/main.log")
// logConfig.Main.Format |= logger.IncludeSystem
// logConfig.EnableSubSystem(spynode.SubSystem)
//
// // Attach the log config to the context.
// ctx := logger.ContextWithLogConfig(context.Background(), logConfig)
//
// Keys for context key/pairs
type loggerkey int
const (
key loggerkey = 1
)
type Level int
type Format int
const (
LevelDebug Level = -2
LevelVerbose Level = -1
LevelInfo Level = 0
LevelWarn Level = 1
LevelError Level = 2
LevelFatal Level = 3 // Calls exit
LevelPanic Level = 4 // Calls panic
FormatJSON Format = 0
FormatText Format = 1
)
// Log entry formatting (which prefix fields to include)
const (
IncludeLevel = 0x01 // level of log entry
IncludeCaller = 0x02 // file name and line number
IncludeDate = 0x04 // date in the local time zone: 2018/01/01
IncludeTime = 0x08 // time in the local time zone: 06:54:32
IncludeMicro = 0x10 // microseconds .123123
IncludeTimeStamp = 0x20 // unix timestamp with microseconds
)
type SetupConfig struct {
Level Level `default:"info" json:"level" envconfig:"LOG_LEVEL"` // Minimum level to log
Format Format `default:"json" json:"format" envconfig:"LOG_FORMAT"`
Path string `json:"path" envconfig:"LOG_PATH"` // File path to write log, otherwise stderr
}
// ContextWithLogSetup returns a context with the specified logging config attached.
func ContextWithLogSetup(ctx context.Context, setup SetupConfig) context.Context {
config := NewConfigFromSetup(setup)
return context.WithValue(ctx, key, config)
}
// ContextWithLogConfig returns a context with the specified logging config attached.
func ContextWithLogConfig(ctx context.Context, config Config) context.Context {
return context.WithValue(ctx, key, config)
}
// ContextWithLogger returns a context with the specified logging attached.
func ContextWithLogger(ctx context.Context, isDevelopment, isText bool,
filePath string) context.Context {
return context.WithValue(ctx, key, NewConfig(isDevelopment, isText, filePath))
}
// ContextWithNoLogger returns a context with no logging
func ContextWithNoLogger(ctx context.Context) context.Context {
return context.WithValue(ctx, key, NewEmptyConfig())
}
// ContextWithLogSubSystem returns a context with the logging subsystem attached.
func ContextWithLogSubSystem(ctx context.Context, subsystem string) context.Context {
configValue := ctx.Value(key)
if configValue == nil {
return context.WithValue(ctx, key, NewEmptyConfig())
}
config, ok := configValue.(Config)
if !ok {
return context.WithValue(ctx, key, NewEmptyConfig())
}
include, includeExists := config.IncludedSubSystems[subsystem]
if !includeExists || !include {
// Empty logger for this subsystem, but leave the rest of the configuration so it can pop
// back up to the main config if it calls through to ContextWithOutLogSubSystem.
n, _ := newEmptySystemConfig()
config = config.Copy()
config.Active = n
return context.WithValue(ctx, key, config)
}
// Log to subsystem specific config
subConfig, subExists := config.SubSystems[subsystem]
if subExists {
config.Active = subConfig.Copy()
return context.WithValue(ctx, key, config)
}
config.Active = config.Main.Copy()
config.Active.addSubSystem(subsystem)
return context.WithValue(ctx, key, config)
}
// ContextWithOutLogSubSystem returns a context with the logging subsystem cleared. Used when a
// context is passed back from a subsystem.
func ContextWithOutLogSubSystem(ctx context.Context) context.Context {
configValue := ctx.Value(key)
if configValue == nil {
// Config not specified. Use default config.
return context.WithValue(ctx, key, NewConfig(false, false, ""))
}
config, ok := configValue.(Config)
if !ok {
// Config invalid. Use default config.
return context.WithValue(ctx, key, NewConfig(false, false, ""))
}
config.Active = config.Main.Copy()
config.Active.removeSubSystem()
return context.WithValue(ctx, key, config)
}
// ContextWithLogTrace returns a context with a trace field added to the logger.
func ContextWithLogTrace(ctx context.Context, trace string) context.Context {
var config *Config
configValue := ctx.Value(key)
if configValue != nil {
contextConfig, ok := configValue.(Config)
if ok {
config = &contextConfig
}
}
if config == nil {
newConfig := NewConfig(false, false, "")
config = &newConfig
}
config.Active.addField(String("trace", trace))
return context.WithValue(ctx, key, *config)
}
// ContextWithLogFields returns a context with a field added to the logger.
func ContextWithLogFields(ctx context.Context, fields ...Field) context.Context {
var config *Config
configValue := ctx.Value(key)
if configValue != nil {
contextConfig, ok := configValue.(Config)
if ok {
config = &contextConfig
}
}
if config == nil {
newConfig := NewConfig(false, false, "")
config = &newConfig
}
for _, field := range fields {
config.Active.addField(field)
}
return context.WithValue(ctx, key, *config)
}
func GetCaller(depth int) string {
_, filepath, line, ok := runtime.Caller(depth + 1)
if ok {
fileParts := strings.Split(filepath, string(os.PathSeparator))
l := len(fileParts)
if l >= 2 {
filepath = fileParts[l-2] + string(os.PathSeparator) + fileParts[l-1]
} else if l != 0 {
filepath = fileParts[0]
}
} else {
filepath = "???"
line = 0
}
return fmt.Sprintf("%s:%d", filepath, line)
}
// Debug adds a debug level entry to the log.
func Debug(ctx context.Context, format string, values ...interface{}) error {
return LogDepth(ctx, LevelDebug, GetCaller(1), format, values...)
}
// Verbose adds a verbose level entry to the log.
func Verbose(ctx context.Context, format string, values ...interface{}) error {
return LogDepth(ctx, LevelVerbose, GetCaller(1), format, values...)
}
// Info adds a info level entry to the log.
func Info(ctx context.Context, format string, values ...interface{}) error {
return LogDepth(ctx, LevelInfo, GetCaller(1), format, values...)
}
// Warn adds a warn level entry to the log.
func Warn(ctx context.Context, format string, values ...interface{}) error {
return LogDepth(ctx, LevelWarn, GetCaller(1), format, values...)
}
// Error adds a error level entry to the log.
func Error(ctx context.Context, format string, values ...interface{}) error {
return LogDepth(ctx, LevelError, GetCaller(1), format, values...)
}
// Fatal adds a fatal level entry to the log and then calls os.Exit(1).
func Fatal(ctx context.Context, format string, values ...interface{}) error {
return LogDepth(ctx, LevelFatal, GetCaller(1), format, values...)
}
// Panic adds a panic level entry to the log and then calls panic().
func Panic(ctx context.Context, format string, values ...interface{}) error {
return LogDepth(ctx, LevelPanic, GetCaller(1), format, values...)
}
// Log an entry to the main Outputs if:
//
// There is no subsystem specified or if the current subsystem is included in the attached
// Config.IncludedSubSystems.
// And the level is equal to or above the specified minimum logging level.
//
// Logs to the Config.SubSystems if the level is above minimum.
func Log(ctx context.Context, level Level, format string, values ...interface{}) error {
return LogDepth(ctx, level, GetCaller(1), format, values...)
}
// LogDepth is the same as Log, but the number of levels above the current call in the stack from
// which to get the file name/line of code can be specified as depth.
func LogDepth(ctx context.Context, level Level, caller string, format string,
values ...interface{}) error {
var config *systemConfig
configValue := ctx.Value(key)
if configValue != nil {
contextConfig, ok := configValue.(Config)
if ok {
config = &contextConfig.Active
}
}
if config == nil {
newConfig, err := newSystemConfig(false, false, "")
if err != nil {
return errors.Wrap(err, "create default config")
}
config = &newConfig
}
return config.writeEntry(level, caller, nil, format, values...)
}
// DebugWithFields adds a debug level entry to the log with the included zap fields.
func DebugWithFields(ctx context.Context, fields []Field, format string,
values ...interface{}) error {
return LogDepthWithFields(ctx, LevelDebug, GetCaller(1), fields, format, values...)
}
// VerboseWithFields adds a verbose level entry to the log with the included zap fields.
func VerboseWithFields(ctx context.Context, fields []Field, format string,
values ...interface{}) error {
return LogDepthWithFields(ctx, LevelVerbose, GetCaller(1), fields, format, values...)
}
// InfoWithFields adds a info level entry to the log with the included zap fields.
func InfoWithFields(ctx context.Context, fields []Field, format string,
values ...interface{}) error {
return LogDepthWithFields(ctx, LevelInfo, GetCaller(1), fields, format, values...)
}
// WarnWithFields adds a warn level entry to the log with the included zap fields.
func WarnWithFields(ctx context.Context, fields []Field, format string,
values ...interface{}) error {
return LogDepthWithFields(ctx, LevelWarn, GetCaller(1), fields, format, values...)
}
// ErrorWithFields adds a error level entry to the log with the included zap fields.
func ErrorWithFields(ctx context.Context, fields []Field, format string,
values ...interface{}) error {
return LogDepthWithFields(ctx, LevelError, GetCaller(1), fields, format, values...)
}
// FatalWithFields adds a fatal level entry to the log with the included zap fields.
func FatalWithFields(ctx context.Context, fields []Field, format string,
values ...interface{}) error {
return LogDepthWithFields(ctx, LevelFatal, GetCaller(1), fields, format, values...)
}
// PanicWithFields adds a panic level entry to the log with the included zap fields.
func PanicWithFields(ctx context.Context, fields []Field, format string,
values ...interface{}) error {
return LogDepthWithFields(ctx, LevelPanic, GetCaller(1), fields, format, values...)
}
// LogDepth is the same as Log, but the number of levels above the current call in the stack from
// which to get the file name/line of code can be specified as depth with the included zap fields.
func LogDepthWithFields(ctx context.Context, level Level, caller string, fields []Field,
format string, values ...interface{}) error {
var config *systemConfig
configValue := ctx.Value(key)
if configValue != nil {
contextConfig, ok := configValue.(Config)
if ok {
config = &contextConfig.Active
}
}
if config == nil {
newConfig, err := newSystemConfig(false, false, "")
if err != nil {
return errors.Wrap(err, "create default config")
}
config = &newConfig
}
return config.writeEntry(level, caller, fields, format, values...)
}
func (v *Level) UnmarshalJSON(data []byte) error {
if len(data) < 2 {
return fmt.Errorf("Too short for level : %d", len(data))
}
value := string(data[1 : len(data)-1])
switch value {
case "debug":
*v = LevelDebug
case "verbose":
*v = LevelVerbose
case "info":
*v = LevelInfo
case "warn":
*v = LevelWarn
case "error":
*v = LevelError
case "fatal":
*v = LevelFatal
case "panic":
*v = LevelPanic
default:
return fmt.Errorf("Unknown level value \"%s\"", value)
}
return nil
}
func (v Level) MarshalJSON() ([]byte, error) {
s := v.String()
if len(s) == 0 {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", s)), nil
}
func (v Level) MarshalText() ([]byte, error) {
switch v {
case LevelDebug:
return []byte("debug"), nil
case LevelVerbose:
return []byte("verbose"), nil
case LevelInfo:
return []byte("info"), nil
case LevelWarn:
return []byte("warn"), nil
case LevelError:
return []byte("error"), nil
case LevelFatal:
return []byte("fatal"), nil
case LevelPanic:
return []byte("panic"), nil
}
return nil, fmt.Errorf("Unknown level value \"%d\"", int(v))
}
func (v *Level) UnmarshalText(text []byte) error {
switch string(text) {
case "debug":
*v = LevelDebug
case "verbose":
*v = LevelVerbose
case "info":
*v = LevelInfo
case "warn":
*v = LevelWarn
case "error":
*v = LevelError
case "fatal":
*v = LevelFatal
case "panic":
*v = LevelPanic
default:
return fmt.Errorf("Unknown level value \"%s\"", string(text))
}
return nil
}
func (v Level) String() string {
switch v {
case LevelDebug:
return "debug"
case LevelVerbose:
return "verbose"
case LevelInfo:
return "info"
case LevelWarn:
return "warn"
case LevelError:
return "error"
case LevelFatal:
return "fatal"
case LevelPanic:
return "panic"
}
return ""
}
func (v *Format) UnmarshalJSON(data []byte) error {
if len(data) < 2 {
return fmt.Errorf("Too short for format : %d", len(data))
}
value := string(data[1 : len(data)-1])
switch value {
case "json":
*v = FormatJSON
case "text":
*v = FormatText
default:
return fmt.Errorf("Unknown format value \"%s\"", value)
}
return nil
}
func (v Format) MarshalJSON() ([]byte, error) {
s := v.String()
if len(s) == 0 {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", s)), nil
}
func (v Format) MarshalText() ([]byte, error) {
switch v {
case FormatJSON:
return []byte("json"), nil
case FormatText:
return []byte("text"), nil
}
return nil, fmt.Errorf("Unknown format value \"%d\"", int(v))
}
func (v *Format) UnmarshalText(text []byte) error {
switch string(text) {
case "json":
*v = FormatJSON
case "text":
*v = FormatText
default:
return fmt.Errorf("Unknown format value \"%s\"", string(text))
}
return nil
}
func (v Format) String() string {
switch v {
case FormatJSON:
return "json"
case FormatText:
return "text"
}
return ""
}