-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathdefault.go
More file actions
276 lines (235 loc) · 7.71 KB
/
default.go
File metadata and controls
276 lines (235 loc) · 7.71 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
package log
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/gofiber/utils/v2"
"github.com/valyala/bytebufferpool"
)
var _ AllLogger[*log.Logger] = (*defaultLogger)(nil)
type defaultLogger struct {
ctx context.Context //nolint:containedctx // stored for deferred field extraction
stdlog *log.Logger
level Level
depth int
}
// writeContextFields appends extracted context key-value pairs to buf.
// Each pair is written as "key=value " (trailing space included).
func (l *defaultLogger) writeContextFields(buf *bytebufferpool.ByteBuffer) {
if l.ctx == nil || len(contextExtractors) == 0 {
return
}
for _, extractor := range contextExtractors {
key, value, ok := extractor(l.ctx)
if ok {
buf.WriteString(key)
buf.WriteByte('=')
buf.WriteString(utils.ToString(value))
buf.WriteByte(' ')
}
}
}
// privateLog logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLog(lv Level, fmtArgs []any) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
buf.WriteString(level)
l.writeContextFields(buf)
fmt.Fprint(buf, fmtArgs...)
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
// privateLogf logs a formatted message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLogf(lv Level, format string, fmtArgs []any) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
buf.WriteString(level)
l.writeContextFields(buf)
if len(fmtArgs) > 0 {
_, _ = fmt.Fprintf(buf, format, fmtArgs...)
} else {
_, _ = fmt.Fprint(buf, format)
}
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
// privateLogw logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLogw(lv Level, format string, keysAndValues []any) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
buf.WriteString(level)
l.writeContextFields(buf)
if format != "" {
buf.WriteString(format)
}
// Write keys and values privateLog buffer
if len(keysAndValues) > 0 {
if (len(keysAndValues) & 1) == 1 {
keysAndValues = append(keysAndValues, "KEYVALS UNPAIRED")
}
for i := 0; i < len(keysAndValues); i += 2 {
if i > 0 || format != "" {
buf.WriteByte(' ')
}
switch key := keysAndValues[i].(type) {
case string:
buf.WriteString(key)
default:
_, _ = fmt.Fprint(buf, key)
}
buf.WriteByte('=')
buf.WriteString(utils.ToString(keysAndValues[i+1]))
}
}
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
if lv == LevelPanic {
panic(buf.String())
}
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
// Trace logs the given values at trace level.
func (l *defaultLogger) Trace(v ...any) {
l.privateLog(LevelTrace, v)
}
// Debug logs the given values at debug level.
func (l *defaultLogger) Debug(v ...any) {
l.privateLog(LevelDebug, v)
}
// Info logs the given values at info level.
func (l *defaultLogger) Info(v ...any) {
l.privateLog(LevelInfo, v)
}
// Warn logs the given values at warn level.
func (l *defaultLogger) Warn(v ...any) {
l.privateLog(LevelWarn, v)
}
// Error logs the given values at error level.
func (l *defaultLogger) Error(v ...any) {
l.privateLog(LevelError, v)
}
// Fatal logs the given values at fatal level and terminates the process.
func (l *defaultLogger) Fatal(v ...any) {
l.privateLog(LevelFatal, v)
}
// Panic logs the given values at panic level and panics.
func (l *defaultLogger) Panic(v ...any) {
l.privateLog(LevelPanic, v)
}
// Tracef formats according to a format specifier and logs at trace level.
func (l *defaultLogger) Tracef(format string, v ...any) {
l.privateLogf(LevelTrace, format, v)
}
// Debugf formats according to a format specifier and logs at debug level.
func (l *defaultLogger) Debugf(format string, v ...any) {
l.privateLogf(LevelDebug, format, v)
}
// Infof formats according to a format specifier and logs at info level.
func (l *defaultLogger) Infof(format string, v ...any) {
l.privateLogf(LevelInfo, format, v)
}
// Warnf formats according to a format specifier and logs at warn level.
func (l *defaultLogger) Warnf(format string, v ...any) {
l.privateLogf(LevelWarn, format, v)
}
// Errorf formats according to a format specifier and logs at error level.
func (l *defaultLogger) Errorf(format string, v ...any) {
l.privateLogf(LevelError, format, v)
}
// Fatalf formats according to a format specifier, logs at fatal level, and terminates the process.
func (l *defaultLogger) Fatalf(format string, v ...any) {
l.privateLogf(LevelFatal, format, v)
}
// Panicf formats according to a format specifier, logs at panic level, and panics.
func (l *defaultLogger) Panicf(format string, v ...any) {
l.privateLogf(LevelPanic, format, v)
}
// Tracew logs at trace level with a message and key/value pairs.
func (l *defaultLogger) Tracew(msg string, keysAndValues ...any) {
l.privateLogw(LevelTrace, msg, keysAndValues)
}
// Debugw logs at debug level with a message and key/value pairs.
func (l *defaultLogger) Debugw(msg string, keysAndValues ...any) {
l.privateLogw(LevelDebug, msg, keysAndValues)
}
// Infow logs at info level with a message and key/value pairs.
func (l *defaultLogger) Infow(msg string, keysAndValues ...any) {
l.privateLogw(LevelInfo, msg, keysAndValues)
}
// Warnw logs at warn level with a message and key/value pairs.
func (l *defaultLogger) Warnw(msg string, keysAndValues ...any) {
l.privateLogw(LevelWarn, msg, keysAndValues)
}
// Errorw logs at error level with a message and key/value pairs.
func (l *defaultLogger) Errorw(msg string, keysAndValues ...any) {
l.privateLogw(LevelError, msg, keysAndValues)
}
// Fatalw logs at fatal level with a message and key/value pairs, then terminates the process.
func (l *defaultLogger) Fatalw(msg string, keysAndValues ...any) {
l.privateLogw(LevelFatal, msg, keysAndValues)
}
// Panicw logs at panic level with a message and key/value pairs, then panics.
func (l *defaultLogger) Panicw(msg string, keysAndValues ...any) {
l.privateLogw(LevelPanic, msg, keysAndValues)
}
// WithContext returns a logger that shares the underlying output but carries
// the provided context. Any registered ContextExtractor functions will be
// called at log time to prepend key-value fields extracted from the context.
func (l *defaultLogger) WithContext(ctx context.Context) CommonLogger {
return &defaultLogger{
stdlog: l.stdlog,
level: l.level,
depth: l.depth - 1,
ctx: ctx,
}
}
// SetLevel updates the minimum level that will be emitted by the logger.
func (l *defaultLogger) SetLevel(level Level) {
l.level = level
}
// SetOutput replaces the underlying writer used by the logger.
func (l *defaultLogger) SetOutput(writer io.Writer) {
l.stdlog.SetOutput(writer)
}
// Logger returns the logger instance. It can be used to adjust the logger configurations in case of need.
func (l *defaultLogger) Logger() *log.Logger {
return l.stdlog
}
// DefaultLogger returns the default logger.
func DefaultLogger[T any]() AllLogger[T] {
if l, ok := logger.(AllLogger[T]); ok {
return l
}
return nil
}