-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
426 lines (341 loc) · 11.1 KB
/
logger.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Package slogx provides extensions to the [slog] package.
// It focuses on performance and simplicity.
// Only functions working with [slog.Attr] are provided.
// Any slower alternatives are not supported.
package slogx
import (
"cmp"
"context"
"log/slog"
"runtime"
"time"
)
// New returns a new [Logger] with the given handler.
func New(handler slog.Handler) *Logger {
return &Logger{commonLogger{
handler: handler,
src: true,
}}
}
// NewContextLogger returns a new [ContextLogger] with the given handler.
func NewContextLogger(handler slog.Handler) *ContextLogger {
return &ContextLogger{commonLogger{
handler: handler,
src: true,
}}
}
// Default returns a new [Logger] with the default handler from [slog.Default].
func Default() *Logger {
return New(defaultHandler())
}
// With returns a new [Logger] based on [Default] with the given attributes.
func With(attrs ...slog.Attr) *Logger {
return Default().With(attrs...)
}
// WithGroup returns a new [Logger] based on [Default] with the given group.
func WithGroup(group string) *Logger {
return Default().WithGroup(group)
}
// Debug logs a message at the debug level.
func Debug(msg string, attrs ...slog.Attr) {
logAttrs(context.Background(), defaultHandler(), slog.LevelDebug, msg, attrs)
}
// Info logs a message at the info level.
func Info(msg string, attrs ...slog.Attr) {
logAttrs(context.Background(), defaultHandler(), slog.LevelInfo, msg, attrs)
}
// Warn logs a message at the warn level.
func Warn(msg string, attrs ...slog.Attr) {
logAttrs(context.Background(), defaultHandler(), slog.LevelWarn, msg, attrs)
}
// Error logs a message at the error level.
func Error(msg string, attrs ...slog.Attr) {
logAttrs(context.Background(), defaultHandler(), slog.LevelError, msg, attrs)
}
// Log logs a message at the given level.
func Log(level slog.Level, msg string, attrs ...slog.Attr) {
logAttrs(context.Background(), defaultHandler(), level, msg, attrs)
}
// ---
// Logger is a simple logger that logs to a [slog.Handler].
// It is an alternative to [slog.Logger] focused on performance and simplicity.
// It forces to use [slog.Attr] for log attributes and does not support slow alternatives provided by [slog.Logger].
// It also takes [slog.Attr] in [Logger.With] because it is the only high performance way to add attributes.
type Logger struct {
commonLogger
}
// Handler returns the logger's handler.
func (l *Logger) Handler() slog.Handler {
return l.handlerForExport()
}
// SlogLogger returns a new [slog.Logger] that logs to the associated handler.
func (l *Logger) SlogLogger() *slog.Logger {
return slog.New(l.handler)
}
// ContextLogger returns a new [ContextLogger] that takes context in logging methods.
func (l *Logger) ContextLogger() *ContextLogger {
return &ContextLogger{l.commonLogger}
}
// Enabled returns true if the given level is enabled.
func (l *Logger) Enabled(ctx context.Context, level slog.Level) bool {
return l.handler.Enabled(ctx, level)
}
// With returns a new [Logger] with the given attributes optimized for short usage (one or two times).
func (l *Logger) With(attrs ...slog.Attr) *Logger {
if len(attrs) != 0 {
l = l.clone()
l.setWithAttrs(attrs)
}
return l
}
// WithLongTerm returns a new [Logger] with the given attributes optimized for long usage.
func (l *Logger) WithLongTerm(attrs ...slog.Attr) *Logger {
if len(attrs) != 0 || l.attrs.Len() != 0 {
l = l.clone()
l.setWithAttrs(attrs)
l.setLongTerm()
}
return l
}
// WithGroup returns a new [Logger] with the given group.
func (l *Logger) WithGroup(group string) *Logger {
if group != "" {
l = l.clone()
l.setWithGroup(group)
}
return l
}
// WithSource returns a new [Logger] that includes the source file and line in the log record if [enabled] is true.
func (l *Logger) WithSource(enabled bool) *Logger {
if l.src != enabled {
l = l.clone()
l.src = enabled
}
return l
}
// Debug logs a message at the debug level.
func (l *Logger) Debug(msg string, attrs ...slog.Attr) {
l.log(context.Background(), slog.LevelDebug, msg, attrs, 0)
}
// DebugContext logs a message at the debug level with the given context.
func (l *Logger) DebugContext(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelDebug, msg, attrs, 0)
}
// Info logs a message at the info level.
func (l *Logger) Info(msg string, attrs ...slog.Attr) {
l.log(context.Background(), slog.LevelInfo, msg, attrs, 0)
}
// InfoContext logs a message at the info level with the given context.
func (l *Logger) InfoContext(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelInfo, msg, attrs, 0)
}
// Warn logs a message at the warn level.
func (l *Logger) Warn(msg string, attrs ...slog.Attr) {
l.log(context.Background(), slog.LevelWarn, msg, attrs, 0)
}
// WarnContext logs a message at the warn level with the given context.
func (l *Logger) WarnContext(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelWarn, msg, attrs, 0)
}
// Error logs a message at the error level.
func (l *Logger) Error(msg string, attrs ...slog.Attr) {
l.log(context.Background(), slog.LevelError, msg, attrs, 0)
}
// ErrorContext logs a message at the error level with the given context.
func (l *Logger) ErrorContext(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelError, msg, attrs, 0)
}
// Log logs a message at the given level.
func (l *Logger) Log(level slog.Level, msg string, attrs ...slog.Attr) {
l.log(context.Background(), level, msg, attrs, 0)
}
// LogContext logs a message at the given level.
func (l *Logger) LogContext(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
l.log(ctx, level, msg, attrs, 0)
}
// LogAttrs logs a message at the given level.
func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
l.log(ctx, level, msg, attrs, 0)
}
// LongTerm returns a new [Logger] with the attributes applied to the handler.
func (l *Logger) LongTerm() *Logger {
if l.attrs.Len() != 0 {
l = l.clone()
l.setLongTerm()
}
return l
}
func (l Logger) clone() *Logger {
return &l
}
// ---
// ContextLogger is an alternative to [Logger] having only methods with context for logging messages.
type ContextLogger struct {
commonLogger
}
// Logger returns a new [Logger] with the associated handler.
func (l *ContextLogger) Logger() *Logger {
return &Logger{l.commonLogger}
}
// Handler returns the associated handler.
func (l *ContextLogger) Handler() slog.Handler {
return l.handlerForExport()
}
// SlogLogger returns a new [slog.Logger] that logs to the associated handler.
func (l *ContextLogger) SlogLogger() *slog.Logger {
return slog.New(l.handler)
}
// Enabled returns true if the given level is enabled.
func (l *ContextLogger) Enabled(ctx context.Context, level slog.Level) bool {
return l.handler.Enabled(ctx, level)
}
// With returns a new [ContextLogger] with the given attributes.
func (l *ContextLogger) With(attrs ...slog.Attr) *ContextLogger {
if len(attrs) != 0 {
l = l.clone()
l.setWithAttrs(attrs)
}
return l
}
// WithLongTerm returns a new [ContextLogger] with the given attributes optimized for multiple usage.
func (l *ContextLogger) WithLongTerm(attrs ...slog.Attr) *ContextLogger {
if len(attrs) != 0 {
l = l.clone()
l.setWithAttrs(attrs)
l.setLongTerm()
}
return l
}
// WithGroup returns a new [ContextLogger] with the given group.
func (l *ContextLogger) WithGroup(group string) *ContextLogger {
if group != "" {
l = l.clone()
l.setWithGroup(group)
}
return l
}
// WithSource returns a new [ContextLogger] that includes the source file and line in the log record if [enabled] is true.
func (l *ContextLogger) WithSource(enabled bool) *ContextLogger {
if l.src != enabled {
l = l.clone()
l.src = enabled
}
return l
}
// Debug logs a message at the debug level.
func (l *ContextLogger) Debug(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelDebug, msg, attrs, 0)
}
// Info logs a message at the info level.
func (l *ContextLogger) Info(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelInfo, msg, attrs, 0)
}
// Warn logs a message at the warn level.
func (l *ContextLogger) Warn(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelWarn, msg, attrs, 0)
}
// Error logs a message at the error level.
func (l *ContextLogger) Error(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelError, msg, attrs, 0)
}
// Log logs a message at the given level.
func (l *ContextLogger) Log(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
l.log(ctx, level, msg, attrs, 0)
}
// LogAttrs logs a message at the given level.
func (l *ContextLogger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
l.log(ctx, level, msg, attrs, 0)
}
// LogWithCallerSkip logs a message at the given level with additional skipping of the specified amount of call stack frames.
func (l *ContextLogger) LogWithCallerSkip(ctx context.Context, skip int, level slog.Level, msg string, attrs ...slog.Attr) {
l.log(ctx, level, msg, attrs, skip)
}
// LongTerm returns a new [Logger] with the attributes applied to the handler.
func (l *ContextLogger) LongTerm() *ContextLogger {
if l.attrs.Len() != 0 {
l = l.clone()
l.setLongTerm()
}
return l
}
func (l ContextLogger) clone() *ContextLogger {
return &l
}
// ---
func defaultHandler() slog.Handler {
return slog.Default().Handler()
}
// ---
type commonLogger struct {
handler slog.Handler
src bool
attrs AttrPack
}
func (l *commonLogger) handlerForExport() slog.Handler {
handler := l.handler
if l.attrs.Len() != 0 {
handler = handler.WithAttrs(l.attrs.Collect())
}
return handler
}
func (l *commonLogger) setWithAttrs(attrs []slog.Attr) {
if len(attrs) != 0 {
l.attrs = l.attrs.Clone()
l.attrs.Add(attrs...)
}
}
func (l *commonLogger) setWithGroup(group string) {
if group != "" {
l.setLongTerm()
l.handler = l.handler.WithGroup(group)
}
}
func (l *commonLogger) setLongTerm() {
if l.attrs.Len() != 0 {
l.handler = l.handlerForExport()
l.attrs = AttrPack{}
}
}
func (l *commonLogger) log(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr, skip int) {
ctx = cmp.Or(ctx, context.Background())
if !l.handler.Enabled(ctx, level) {
return
}
var pcs [1]uintptr
if l.src {
runtime.Callers(skip+3, pcs[:])
}
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
if l.attrs.Len() != 0 {
l.attrs.Enumerate(func(attr slog.Attr) bool {
r.AddAttrs(attr)
return true
})
}
r.AddAttrs(attrs...)
_ = l.handler.Handle(ctx, r)
}
// ---
func logAttrs(ctx context.Context, handler slog.Handler, level slog.Level, msg string, attrs []slog.Attr) {
l := commonLogger{
handler: handler,
src: true,
}
l.log(ctx, level, msg, attrs, 1)
}
// ---
type commonLoggerInterface[T any] interface {
Handler() slog.Handler
SlogLogger() *slog.Logger
Enabled(context.Context, slog.Level) bool
LogAttrs(context.Context, slog.Level, string, ...slog.Attr)
With(...slog.Attr) T
WithLongTerm(...slog.Attr) T
WithGroup(string) T
WithSource(bool) T
LongTerm() T
}
var (
_ commonLoggerInterface[*Logger] = (*Logger)(nil)
_ commonLoggerInterface[*ContextLogger] = (*ContextLogger)(nil)
)