-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_impl.go
195 lines (153 loc) · 5.15 KB
/
logger_impl.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
package log
import (
"fmt"
"github.com/echocat/slf4g/level"
"github.com/echocat/slf4g/fields"
)
type loggerImpl struct {
coreProvider func() CoreLogger
fields fields.Fields
}
func (instance *loggerImpl) Unwrap() CoreLogger {
return instance.coreProvider()
}
func (instance *loggerImpl) GetName() string {
return instance.Unwrap().GetName()
}
func (instance *loggerImpl) Log(event Event, skipFrames uint16) {
instance.Unwrap().Log(event, skipFrames+1)
}
func (instance *loggerImpl) NewEvent(l level.Level, values map[string]interface{}) Event {
if v := instance.fields; v != nil {
return instance.NewEventWithFields(l, v.WithAll(values))
}
return instance.Unwrap().NewEvent(l, values)
}
func (instance *loggerImpl) NewEventWithFields(l level.Level, f fields.ForEachEnabled) Event {
target := instance.Unwrap()
if wf, ok := target.(interface {
NewEventWithFields(l level.Level, f fields.ForEachEnabled) Event
}); ok {
return wf.NewEventWithFields(l, f)
}
asMap, err := fields.AsMap(f)
if err != nil {
panic(fmt.Errorf("cannot make a map out of %v: %w", f, err))
}
return target.NewEvent(l, asMap)
}
func (instance *loggerImpl) Accepts(event Event) bool {
return instance.Unwrap().Accepts(event)
}
func (instance *loggerImpl) IsLevelEnabled(level level.Level) bool {
return instance.Unwrap().IsLevelEnabled(level)
}
func (instance *loggerImpl) GetProvider() Provider {
return instance.Unwrap().GetProvider()
}
func (instance *loggerImpl) log(level level.Level, args ...interface{}) {
instance.DoLog(level, 2, args...)
}
func (instance *loggerImpl) logf(level level.Level, format string, args ...interface{}) {
instance.DoLogf(level, 2, format, args...)
}
func (instance *loggerImpl) DoLog(level level.Level, skipFrames uint16, args ...interface{}) {
if !instance.IsLevelEnabled(level) {
return
}
provider := instance.GetProvider()
e := instance.NewEventWithFields(level, instance.fields)
if len(args) == 1 {
e = e.With(provider.GetFieldKeysSpec().GetMessage(), args[0])
} else if len(args) > 1 {
e = e.With(provider.GetFieldKeysSpec().GetMessage(), args)
}
instance.Unwrap().Log(e, skipFrames+1)
}
func (instance *loggerImpl) DoLogf(level level.Level, skipFrames uint16, format string, args ...interface{}) {
if !instance.IsLevelEnabled(level) {
return
}
provider := instance.GetProvider()
e := instance.NewEventWithFields(level, instance.fields).
Withf(provider.GetFieldKeysSpec().GetMessage(), format, args...)
instance.Unwrap().Log(e, skipFrames+1)
}
func (instance *loggerImpl) Trace(args ...interface{}) {
instance.log(level.Trace, args...)
}
func (instance *loggerImpl) Tracef(format string, args ...interface{}) {
instance.logf(level.Trace, format, args...)
}
func (instance *loggerImpl) IsTraceEnabled() bool {
return instance.IsLevelEnabled(level.Trace)
}
func (instance *loggerImpl) Debug(args ...interface{}) {
instance.log(level.Debug, args...)
}
func (instance *loggerImpl) Debugf(format string, args ...interface{}) {
instance.logf(level.Debug, format, args...)
}
func (instance *loggerImpl) IsDebugEnabled() bool {
return instance.IsLevelEnabled(level.Debug)
}
func (instance *loggerImpl) Info(args ...interface{}) {
instance.log(level.Info, args...)
}
func (instance *loggerImpl) Infof(format string, args ...interface{}) {
instance.logf(level.Info, format, args...)
}
func (instance *loggerImpl) IsInfoEnabled() bool {
return instance.IsLevelEnabled(level.Info)
}
func (instance *loggerImpl) Warn(args ...interface{}) {
instance.log(level.Warn, args...)
}
func (instance *loggerImpl) Warnf(format string, args ...interface{}) {
instance.logf(level.Warn, format, args...)
}
func (instance *loggerImpl) IsWarnEnabled() bool {
return instance.IsLevelEnabled(level.Warn)
}
func (instance *loggerImpl) Error(args ...interface{}) {
instance.log(level.Error, args...)
}
func (instance *loggerImpl) Errorf(format string, args ...interface{}) {
instance.logf(level.Error, format, args...)
}
func (instance *loggerImpl) IsErrorEnabled() bool {
return instance.IsLevelEnabled(level.Error)
}
func (instance *loggerImpl) Fatal(args ...interface{}) {
instance.log(level.Fatal, args...)
}
func (instance *loggerImpl) Fatalf(format string, args ...interface{}) {
instance.logf(level.Fatal, format, args...)
}
func (instance *loggerImpl) IsFatalEnabled() bool {
return instance.IsLevelEnabled(level.Fatal)
}
func (instance *loggerImpl) With(name string, value interface{}) Logger {
return &loggerImpl{
coreProvider: instance.coreProvider,
fields: instance.fields.With(name, value),
}
}
func (instance *loggerImpl) Withf(name string, format string, args ...interface{}) Logger {
return instance.With(name, fields.LazyFormat(format, args...))
}
func (instance *loggerImpl) WithError(err error) Logger {
return instance.With(instance.GetProvider().GetFieldKeysSpec().GetError(), err)
}
func (instance *loggerImpl) WithAll(of map[string]interface{}) Logger {
return &loggerImpl{
coreProvider: instance.coreProvider,
fields: instance.fields.WithAll(of),
}
}
func (instance *loggerImpl) Without(keys ...string) Logger {
return &loggerImpl{
coreProvider: instance.coreProvider,
fields: instance.fields.Without(keys...),
}
}