forked from rockbears/log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
178 lines (144 loc) · 3.75 KB
/
log.go
File metadata and controls
178 lines (144 loc) · 3.75 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
package log
import (
"context"
"fmt"
"runtime"
"sort"
"github.com/pkg/errors"
)
func RegisterField(fields ...Field) {
registeredFieldsMutex.Lock()
defer registeredFieldsMutex.Unlock()
for _, f := range fields {
var exist bool
for _, existingF := range registeredFields {
if f == existingF {
exist = true
break
}
}
if !exist {
registeredFields = append(registeredFields, f)
}
}
sort.Slice(registeredFields, func(i, j int) bool {
return registeredFields[i] < registeredFields[j]
})
}
func UnregisterField(fields ...Field) {
registeredFieldsMutex.Lock()
defer registeredFieldsMutex.Unlock()
loop:
for _, f := range fields {
for i, existingF := range registeredFields {
if f == existingF {
registeredFields = append(registeredFields[:i], registeredFields[i+1:]...)
goto loop
}
}
}
sort.Slice(registeredFields, func(i, j int) bool {
return registeredFields[i] < registeredFields[j]
})
}
// CallerFrameToSkip correspond to the number of frame to skip while retrieving the caller stack
var CallerFrameToSkip = 2
func Skip(field Field, value interface{}) {
excludeRulesMutex.Lock()
defer excludeRulesMutex.Unlock()
if excludeRules == nil {
excludeRules = make(map[Field]any)
}
excludeRules[field] = value
}
func Debug(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelDebug, format, args...)
}
func Info(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelInfo, format, args...)
}
func Warn(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelWarn, format, args...)
}
func Error(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelError, format, args...)
}
func Fatal(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelFatal, format, args...)
}
func Panic(ctx context.Context, format string, args ...interface{}) {
call(ctx, LevelPanic, format, args...)
}
var (
FieldSourceFile = Field("source_file")
FieldSourceLine = Field("source_line")
FieldCaller = Field("caller")
FieldStackTrace = Field("stack_trace")
)
func init() {
RegisterField(FieldSourceFile, FieldSourceLine, FieldCaller, FieldStackTrace)
}
func call(ctx context.Context, level Level, format string, args ...interface{}) {
entry := Factory()
if level < entry.GetLevel() {
return
}
pc, file, line, ok := runtime.Caller(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())
}
}
for _, k := range registeredFields {
v := ctx.Value(k)
if v != nil {
if exludeValue, has := excludeRules[k]; has {
if v == exludeValue {
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...)
}
}
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 ErrorWithStackTrace(ctx context.Context, err error) {
ctx = ContextWithStackTrace(ctx, err)
call(ctx, LevelError, err.Error())
}
func FieldValues(ctx context.Context) map[Field]interface{} {
res := make(map[Field]interface{}, 10)
for _, k := range registeredFields {
v := ctx.Value(k)
if v != nil {
res[k] = v
}
}
return res
}