-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler_test.go
198 lines (166 loc) · 5.22 KB
/
handler_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
package sloggokit_test
import (
"bytes"
"fmt"
"log/slog"
"reflect"
"regexp"
"strings"
"testing"
"testing/slogtest"
"time"
"github.com/go-kit/log"
"github.com/go-logfmt/logfmt"
"github.com/stretchr/testify/require"
slgk "github.com/tjhop/slog-gokit"
)
var (
logRegexp = regexp.MustCompile(`level=(?P<LevelValue>warn|info|error|debug).*time=.+msg=.+`)
)
func TestNewGoKitHandler(t *testing.T) {
t.Run("nil level", func(t *testing.T) {
var buf bytes.Buffer
h := slgk.NewGoKitHandler(log.NewLogfmtLogger(&buf), nil)
results := func() []map[string]any {
var ms []map[string]any
// Print logs for humans.
fmt.Println(buf.String())
// wrap the buffer in a new reader as pre-go 1.24 the
// slogtest package calls results() multiple times, which
// causes the tests to pass _without running checks_.
// See /~https://github.com/golang/go/issues/67605
dec := logfmt.NewDecoder(strings.NewReader(buf.String()))
for dec.ScanRecord() {
m := map[string]any{}
for dec.ScanKeyval() {
k, v, err := parseValue(string(dec.Key()), dec.Value())
require.NoError(t, err)
// If it's a map, merge it with the current map
if m[k] != nil && reflect.TypeOf(m[k]).Kind() == reflect.Map {
m[k] = mergeMaps(m[k].(map[string]any), v.(map[string]any))
continue
}
m[k] = v
}
ms = append(ms, m)
}
err := dec.Err()
require.NoError(t, err, "failed to decode logfmt entry")
return ms
}
err := slogtest.TestHandler(h, results)
require.NoError(t, err, "failed slog handler test suite")
})
t.Run("debug level", func(t *testing.T) {
var buf bytes.Buffer
lvl := &slog.LevelVar{}
lvl.Set(slog.LevelDebug)
h := slgk.NewGoKitHandler(log.NewLogfmtLogger(&buf), lvl)
results := func() []map[string]any {
var ms []map[string]any
// Print logs for humans.
fmt.Println(buf.String())
dec := logfmt.NewDecoder(strings.NewReader(buf.String()))
for dec.ScanRecord() {
m := map[string]any{}
for dec.ScanKeyval() {
k, v, err := parseValue(string(dec.Key()), dec.Value())
require.NoError(t, err)
// If it's a map, merge it with the current map
if m[k] != nil && reflect.TypeOf(m[k]).Kind() == reflect.Map {
m[k] = mergeMaps(m[k].(map[string]any), v.(map[string]any))
continue
}
m[k] = v
}
ms = append(ms, m)
}
err := dec.Err()
require.NoError(t, err, "failed to decode logfmt entry")
return ms
}
err := slogtest.TestHandler(h, results)
require.NoError(t, err, "failed slog handler test suite")
})
t.Run("dynamic level", func(t *testing.T) {
var buf bytes.Buffer
lvl := &slog.LevelVar{}
gklogger := log.NewLogfmtLogger(&buf)
h := slgk.NewGoKitHandler(gklogger, lvl)
slogger := slog.New(h)
wantedLevelCounts := map[string]int{"info": 1, "debug": 1}
// Start at debug level.
lvl.Set(slog.LevelDebug)
slogger.Info("info", "hello", "world")
slogger.Debug("debug", "hello", "world")
// We expect to see one of each log level type in `wantedLevelCounts`
counts := getLogEntryLevelCounts(buf.String(), logRegexp)
require.Equal(t, wantedLevelCounts["info"], counts["info"], "info log successfully detected")
require.Equal(t, wantedLevelCounts["debug"], counts["debug"], "debug log successfully detected")
// Print logs for humans.
fmt.Println(buf.String())
buf.Reset()
// Test that log level can be adjusted on-the-fly to info and
// that a subsequent call to write a debug level log is _not_
// written to the file.
lvl.Set(slog.LevelInfo)
slogger.Info("info", "hello", "world")
slogger.Debug("debug", "hello", "world")
// We expect to see one info log, and 0 debug logs.
counts = getLogEntryLevelCounts(buf.String(), logRegexp)
require.Equal(t, wantedLevelCounts["info"], counts["info"], "info log successfully detected")
require.NotEqual(t, wantedLevelCounts["debug"], counts["debug"], "extra debug log detected")
// Print logs for humans to see, if needed.
fmt.Println(buf.String())
buf.Reset()
})
}
func getLogEntryLevelCounts(s string, re *regexp.Regexp) map[string]int {
counters := make(map[string]int)
lines := strings.Split(s, "\n")
for _, line := range lines {
matches := re.FindStringSubmatch(line)
if len(matches) > 1 {
levelIndex := re.SubexpIndex("LevelValue")
counters[strings.ToLower(matches[levelIndex])]++
}
}
return counters
}
func mergeMaps(m1, m2 map[string]any) map[string]any {
for k, v := range m2 {
if m1[k] != nil && reflect.TypeOf(m1[k]).Kind() == reflect.Map {
m1[k] = mergeMaps(m1[k].(map[string]any), v.(map[string]any))
continue
}
m1[k] = v
}
return m1
}
func parseValue(key string, value []byte) (string, any, error) {
switch key {
case "level":
var l slog.Level
err := l.UnmarshalText([]byte(value))
if err != nil {
return key, nil, err
}
return key, l, nil
case "time":
// parse timestamp in iso8601 2025-02-20T16:58:30.683457-05:00
parsedTime, err := time.Parse(time.RFC3339Nano, string(value))
if err != nil {
return key, nil, err
}
return key, parsedTime, nil
}
groups := strings.SplitN(key, ".", 2)
if len(groups) != 2 {
return key, string(value), nil
}
k, v, err := parseValue(groups[1], value)
if err != nil {
return key, nil, err
}
return groups[0], map[string]any{k: v}, nil
}