-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathslog_test.go
96 lines (80 loc) · 1.82 KB
/
slog_test.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
package bench
import (
"context"
"fmt"
"io"
"log/slog"
)
func slogAttrs() []slog.Attr {
return []slog.Attr{
slog.Int("bytes", ctxBodyBytes),
slog.String("request", ctxRequest),
slog.Float64("elapsed_time_ms", ctxTimeElapsedMs),
slog.Any("user", ctxUser),
slog.Time("now", ctxTime),
slog.Any("months", ctxMonths),
slog.Any("primes", ctxFirst10Primes),
slog.Any("users", ctxUsers),
slog.Any("error", ctxErr),
}
}
func newSlog(w io.Writer) *slog.Logger {
return slog.New(slog.NewJSONHandler(w, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
}
func newSlogWithCtx(w io.Writer, attr []slog.Attr) *slog.Logger {
return slog.New(slog.NewJSONHandler(w, &slog.HandlerOptions{
Level: slog.LevelInfo,
}).WithAttrs(attr))
}
type slogBench struct {
l *slog.Logger
}
func (b *slogBench) new(w io.Writer) logBenchmark {
return &slogBench{
l: newSlog(w),
}
}
func (b *slogBench) newWithCtx(w io.Writer) logBenchmark {
return &slogBench{
l: newSlogWithCtx(w, slogAttrs()),
}
}
func (b *slogBench) name() string {
return "Slog"
}
func (b *slogBench) logEvent(msg string) {
b.l.Info(msg)
}
func (b *slogBench) logEventFmt(msg string, args ...any) {
b.l.Info(fmt.Sprintf(msg, args...))
}
func (b *slogBench) logEventCtx(msg string) {
b.l.LogAttrs(
context.Background(),
slog.LevelInfo,
msg,
slogAttrs()...,
)
}
func (b *slogBench) logEventCtxWeak(msg string) {
b.l.Info(msg, alternatingKeyValuePairs()...)
}
func (b *slogBench) logDisabled(msg string) {
b.l.Debug(msg)
}
func (b *slogBench) logDisabledFmt(msg string, args ...any) {
b.l.Debug(fmt.Sprintf(msg, args...))
}
func (b *slogBench) logDisabledCtx(msg string) {
b.l.LogAttrs(
context.Background(),
slog.LevelDebug,
msg,
slogAttrs()...,
)
}
func (b *slogBench) logDisabledCtxWeak(msg string) {
b.l.Debug(msg, alternatingKeyValuePairs()...)
}