-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathslog_test.go
199 lines (169 loc) · 4.7 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
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
package logbench
import (
"context"
"fmt"
"io"
"log/slog"
"strings"
"time"
)
func init() {
tests["slog"] = slogTester{}
}
type slogTester struct {
l slog.Logger
}
var (
_ logTesterArray = (*slogTester)(nil)
)
func (slogTester) newLogger(out io.Writer, disabled bool) logTester {
lvl := slog.LevelInfo
testFixup := func(groups []string, a slog.Attr) slog.Attr {
switch a.Key {
case "time":
return slog.Attr{} // discard timestamps
case "replaceTime-time":
return slog.Attr{Key: "time", Value: slog.Int64Value(a.Value.Time().Unix())}
case "replaceTimes-time":
anyTimes := a.Value.Any().([]time.Time)
anyUnixTimes := make([]int64, len(anyTimes))
for i, anyTime := range anyTimes {
anyUnixTimes[i] = anyTime.Unix()
}
return slog.Attr{Key: "time", Value: slog.AnyValue(anyUnixTimes)}
case "duration":
anyValue := a.Value.Any()
switch anyValue := anyValue.(type) {
case time.Duration:
return slog.Attr{Key: "duration", Value: slog.IntValue(int(anyValue.Milliseconds()))}
case []time.Duration:
anyDurations := anyValue
anyIntDurations := make([]int, len(anyDurations))
for i, anyTime := range anyDurations {
anyIntDurations[i] = int(anyTime.Milliseconds())
}
return slog.Attr{Key: "duration", Value: slog.AnyValue(anyIntDurations)}
default:
return a
}
case "level":
lowerValue := slog.StringValue(strings.ToLower(a.Value.String()))
return slog.Attr{Key: a.Key, Value: lowerValue}
case "msg":
return slog.Attr{Key: "message", Value: a.Value}
case "errors":
valueErrors := a.Value.Any().([]error)
errorStrings := make([]string, len(valueErrors))
for i, err := range valueErrors {
errorStrings[i] = err.Error()
}
return slog.Attr{Key: "errors", Value: slog.AnyValue(errorStrings)}
default:
return a
}
}
var handler slog.Handler = slog.NewJSONHandler(out, &slog.HandlerOptions{Level: lvl, ReplaceAttr: testFixup})
if disabled {
handler = disabledHandler{}
}
return slogTester{*slog.New(handler)}
}
func (t slogTester) logMsg(msg string) {
t.l.Info(msg)
}
func (t slogTester) logFormat(format string, v ...interface{}) bool {
t.l.Info(fmt.Sprintf(format, v...))
return true
}
func (t slogTester) withContext(context map[string]interface{}) (logTester, bool) {
var args = make([]any, len(context)*2)
index := 0
for s, i := range context {
args[index] = s
args[index+1] = i
index += 2
}
return slogTester{*t.l.With(args...)}, true
}
func (t slogTester) logBool(msg, key string, value bool) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logInt(msg, key string, value int) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logFloat32(msg, key string, value float32) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logFloat64(msg, key string, value float64) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logTime(msg, key string, value time.Time) bool {
t.l.Info(msg, "replaceTime-"+key, value)
return true
}
func (t slogTester) logDuration(msg, key string, value time.Duration) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logError(msg, key string, value error) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logString(msg, key string, value string) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logObject(msg, key string, value *obj) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logBools(msg, key string, value []bool) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logInts(msg, key string, value []int) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logFloats32(msg, key string, value []float32) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logFloats64(msg, key string, value []float64) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logTimes(msg, key string, value []time.Time) bool {
t.l.Info(msg, "replaceTimes-"+key, value)
return true
}
func (t slogTester) logDurations(msg, key string, value []time.Duration) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logErrors(msg, key string, value []error) bool {
t.l.Info(msg, key, value)
return true
}
func (t slogTester) logStrings(msg, key string, value []string) bool {
t.l.Info(msg, key, value)
return true
}
type disabledHandler struct {
}
func (d disabledHandler) Enabled(ctx context.Context, level slog.Level) bool {
return false
}
func (d disabledHandler) Handle(ctx context.Context, record slog.Record) error {
return nil
}
func (d disabledHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return d
}
func (d disabledHandler) WithGroup(name string) slog.Handler {
return d
}