forked from logur/logur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_fields.go
131 lines (103 loc) · 3.7 KB
/
logger_fields.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
package logur
import (
"context"
)
// WithFields returns a new logger instance that attaches the given fields to every subsequent log call.
func WithFields(logger Logger, fields map[string]interface{}) LoggerFacade {
loggerFacade := ensureLoggerFacade(logger)
if len(fields) == 0 {
return loggerFacade
}
// Do not add a new layer
// Create a new logger instead with the parent fields
//
// fieldLogger already implements LoggerFacade, so loggerFacade should be the same as logger if it's a fieldLogger
if l, ok := loggerFacade.(*fieldLogger); ok && len(l.fields) > 0 {
_fields := make(map[string]interface{}, len(l.fields)+len(fields))
for key, value := range l.fields {
_fields[key] = value
}
for key, value := range fields {
_fields[key] = value
}
fields = _fields
logger = l.logger
}
l := &fieldLogger{logger: loggerFacade, fields: fields}
if levelEnabler, ok := logger.(LevelEnabler); ok {
l.levelEnabler = levelEnabler
}
return l
}
// WithField is a shortcut for WithFields(logger, map[string]interface{}{key: value}).
func WithField(logger Logger, key string, value interface{}) LoggerFacade {
return WithFields(logger, map[string]interface{}{key: value})
}
// fieldLogger holds a context and passes it to the underlying logger when a log event is recorded.
type fieldLogger struct {
logger LoggerFacade
fields map[string]interface{}
levelEnabler LevelEnabler
}
// Trace implements the logur.Logger interface.
func (l *fieldLogger) Trace(msg string, fields ...map[string]interface{}) {
l.log(Trace, l.logger.Trace, msg, fields)
}
// Debug implements the logur.Logger interface.
func (l *fieldLogger) Debug(msg string, fields ...map[string]interface{}) {
l.log(Debug, l.logger.Debug, msg, fields)
}
// Info implements the logur.Logger interface.
func (l *fieldLogger) Info(msg string, fields ...map[string]interface{}) {
l.log(Info, l.logger.Info, msg, fields)
}
// Warn implements the logur.Logger interface.
func (l *fieldLogger) Warn(msg string, fields ...map[string]interface{}) {
l.log(Warn, l.logger.Warn, msg, fields)
}
// Error implements the logur.Logger interface.
func (l *fieldLogger) Error(msg string, fields ...map[string]interface{}) {
l.log(Error, l.logger.Error, msg, fields)
}
// log deduplicates some field logger code.
func (l *fieldLogger) log(level Level, fn LogFunc, msg string, fields []map[string]interface{}) {
if !l.levelEnabled(level) {
return
}
fn(msg, mergeFields(l.fields, fields))
}
func (l *fieldLogger) TraceContext(ctx context.Context, msg string, fields ...map[string]interface{}) {
l.logContext(Trace, l.logger.TraceContext, ctx, msg, fields)
}
func (l *fieldLogger) DebugContext(ctx context.Context, msg string, fields ...map[string]interface{}) {
l.logContext(Debug, l.logger.DebugContext, ctx, msg, fields)
}
func (l *fieldLogger) InfoContext(ctx context.Context, msg string, fields ...map[string]interface{}) {
l.logContext(Info, l.logger.InfoContext, ctx, msg, fields)
}
func (l *fieldLogger) WarnContext(ctx context.Context, msg string, fields ...map[string]interface{}) {
l.logContext(Warn, l.logger.WarnContext, ctx, msg, fields)
}
func (l *fieldLogger) ErrorContext(ctx context.Context, msg string, fields ...map[string]interface{}) {
l.logContext(Error, l.logger.ErrorContext, ctx, msg, fields)
}
// logContext deduplicates some field logger code.
// nolint: golint
func (l *fieldLogger) logContext(
level Level,
fn LogContextFunc,
ctx context.Context,
msg string,
fields []map[string]interface{},
) {
if !l.levelEnabled(level) {
return
}
fn(ctx, msg, mergeFields(l.fields, fields))
}
func (l *fieldLogger) levelEnabled(level Level) bool {
if l.levelEnabler != nil {
return l.levelEnabler.LevelEnabled(level)
}
return true
}