-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
323 lines (256 loc) · 6.8 KB
/
log.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
312
313
314
315
316
317
318
319
320
321
322
323
package log
import (
"context"
"fmt"
"runtime"
"sort"
"sync"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
FieldSourceFile = Field("source_file")
FieldSourceLine = Field("source_line")
FieldCaller = Field("caller")
FieldStackTrace = Field("stack_trace")
)
var global *Logger
var Factory = NewLogrusWrapper(logrus.StandardLogger())
func init() {
global = New()
global.callerFrameToSkip = 3
}
type Logger struct {
registeredFields []Field
excludeRules []ExcludeRule
factory WrapperFactoryFunc
callerFrameToSkip int
mutex sync.RWMutex
}
func New() *Logger {
return NewWithFactory(nil)
}
func NewWithFactory(factory WrapperFactoryFunc) *Logger {
logger := &Logger{factory: factory, callerFrameToSkip: 2}
logger.RegisterDefaultFields()
return logger
}
func (l *Logger) GetFramesToSkip() int {
l.mutex.RLock()
defer l.mutex.RUnlock()
return l.callerFrameToSkip
}
func (l *Logger) SetFramesToSkip(s int) {
l.mutex.Lock()
defer l.mutex.Unlock()
l.callerFrameToSkip = s
}
func (l *Logger) RegisterField(fields ...Field) {
l.mutex.Lock()
defer l.mutex.Unlock()
for _, f := range fields {
var exist bool
for _, existingF := range l.registeredFields {
if f == existingF {
exist = true
break
}
}
if !exist {
l.registeredFields = append(l.registeredFields, f)
}
}
sort.Slice(l.registeredFields, func(i, j int) bool {
return l.registeredFields[i] < l.registeredFields[j]
})
}
func (l *Logger) UnregisterField(fields ...Field) {
l.mutex.Lock()
defer l.mutex.Unlock()
loop:
for _, f := range fields {
for i, existingF := range l.registeredFields {
if f == existingF {
l.registeredFields = append(l.registeredFields[:i], l.registeredFields[i+1:]...)
goto loop
}
}
}
sort.Slice(l.registeredFields, func(i, j int) bool {
return l.registeredFields[i] < l.registeredFields[j]
})
}
func (l *Logger) GetRegisteredFields() []Field {
l.mutex.RLock()
defer l.mutex.RUnlock()
fields := make([]Field, len(l.registeredFields))
copy(fields, l.registeredFields)
return fields
}
func (l *Logger) GetExcludeRules() []ExcludeRule {
l.mutex.RLock()
defer l.mutex.RUnlock()
excludeRules := make([]ExcludeRule, len(l.excludeRules))
copy(excludeRules, l.excludeRules)
return excludeRules
}
func (l *Logger) RegisterDefaultFields() {
l.RegisterField(FieldSourceFile, FieldSourceLine, FieldCaller, FieldStackTrace)
}
func (l *Logger) Skip(field Field, value interface{}) {
l.mutex.Lock()
defer l.mutex.Unlock()
for i := range l.excludeRules {
if l.excludeRules[i].Field == field {
l.excludeRules[i].Value = value
return
}
}
l.excludeRules = append(l.excludeRules, ExcludeRule{field, value})
}
func (l *Logger) SetFactory(factory WrapperFactoryFunc) {
l.mutex.Lock()
defer l.mutex.Unlock()
l.factory = factory
}
func (l *Logger) Debug(ctx context.Context, format string, args ...interface{}) {
l.call(ctx, LevelDebug, format, args...)
}
func (l *Logger) Info(ctx context.Context, format string, args ...interface{}) {
l.call(ctx, LevelInfo, format, args...)
}
func (l *Logger) Warn(ctx context.Context, format string, args ...interface{}) {
l.call(ctx, LevelWarn, format, args...)
}
func (l *Logger) Error(ctx context.Context, format string, args ...interface{}) {
l.call(ctx, LevelError, format, args...)
}
func (l *Logger) Fatal(ctx context.Context, format string, args ...interface{}) {
l.call(ctx, LevelFatal, format, args...)
}
func (l *Logger) Panic(ctx context.Context, format string, args ...interface{}) {
l.call(ctx, LevelPanic, format, args...)
}
func (l *Logger) call(ctx context.Context, level Level, format string, args ...interface{}) {
var entry Wrapper
l.mutex.RLock()
if l.factory == nil {
entry = Factory()
} else {
entry = l.factory()
}
l.mutex.RUnlock()
if level < entry.GetLevel() {
return
}
pc, file, line, ok := runtime.Caller(l.callerFrameToSkip)
if ok {
ctx = context.WithValue(ctx, FieldSourceFile, file)
ctx = context.WithValue(ctx, FieldSourceLine, line)
details := runtime.FuncForPC(pc)
if details != nil {
ctx = context.WithValue(ctx, FieldCaller, details.Name())
}
}
mExcludeRules := make(map[Field]any)
for _, rule := range l.GetExcludeRules() {
mExcludeRules[rule.Field] = rule.Value
}
for _, k := range l.GetRegisteredFields() {
v := ctx.Value(k)
if v != nil {
if excludeValue, has := mExcludeRules[k]; has {
if v == excludeValue {
return
}
}
entry.WithField(string(k), v)
}
}
switch level {
case LevelInfo:
entry.Infof(format, args...)
case LevelWarn:
entry.Warnf(format, args...)
case LevelError:
entry.Errorf(format, args...)
case LevelFatal:
entry.Fatalf(format, args...)
case LevelPanic:
entry.Panicf(format, args...)
default:
entry.Debugf(format, args...)
}
}
func (l *Logger) ErrorWithStackTrace(ctx context.Context, err error) {
ctx = ContextWithStackTrace(ctx, err)
l.call(ctx, LevelError, err.Error())
}
func (l *Logger) FieldValues(ctx context.Context) map[Field]interface{} {
res := make(map[Field]interface{}, 10)
for _, k := range l.registeredFields {
v := ctx.Value(k)
if v != nil {
res[k] = v
}
}
return res
}
type StackTracer interface {
StackTrace() errors.StackTrace
}
func ContextWithStackTrace(ctx context.Context, err error) context.Context {
errWithStracktrace, ok := err.(StackTracer)
if ok {
ctx = context.WithValue(ctx, FieldStackTrace, fmt.Sprintf("%+v", errWithStracktrace))
}
return ctx
}
func GetFramesToSkip() int {
return global.GetFramesToSkip()
}
func SetFramesToSkip(s int) {
global.SetFramesToSkip(s)
}
func RegisterField(fields ...Field) {
global.RegisterField(fields...)
}
func UnregisterField(fields ...Field) {
global.UnregisterField(fields...)
}
func GetRegisteredFields() []Field {
return global.GetRegisteredFields()
}
func RegisterDefaultFields() {
global.RegisterDefaultFields()
}
func Skip(field Field, value interface{}) {
global.Skip(field, value)
}
func SetFactory(factory WrapperFactoryFunc) {
global.SetFactory(factory)
}
func Debug(ctx context.Context, format string, args ...interface{}) {
global.Debug(ctx, format, args...)
}
func Info(ctx context.Context, format string, args ...interface{}) {
global.Info(ctx, format, args...)
}
func Warn(ctx context.Context, format string, args ...interface{}) {
global.Warn(ctx, format, args...)
}
func Error(ctx context.Context, format string, args ...interface{}) {
global.Error(ctx, format, args...)
}
func Fatal(ctx context.Context, format string, args ...interface{}) {
global.Fatal(ctx, format, args...)
}
func Panic(ctx context.Context, format string, args ...interface{}) {
global.Panic(ctx, format, args...)
}
func ErrorWithStackTrace(ctx context.Context, err error) {
global.ErrorWithStackTrace(ctx, err)
}
func FieldValues(ctx context.Context) map[Field]interface{} {
return global.FieldValues(ctx)
}