-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_test.go
142 lines (114 loc) · 3.35 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
package sloggokit
import (
"bytes"
"fmt"
"log/slog"
"regexp"
"strings"
"testing"
"testing/slogtest"
"github.com/go-kit/log"
"github.com/go-logfmt/logfmt"
"github.com/stretchr/testify/require"
)
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 := NewGoKitHandler(log.NewLogfmtLogger(&buf), nil)
results := func() []map[string]any {
var ms []map[string]any
// Print logs for humans.
fmt.Println(buf.String())
dec := logfmt.NewDecoder(&buf)
for dec.ScanRecord() {
m := make(map[string]any)
for dec.ScanKeyval() {
m[string(dec.Key())] = dec.Value()
}
ms = append(ms, m)
}
if err := dec.Err(); err != nil {
t.Fatal(err)
}
return ms
}
err := slogtest.TestHandler(h, results)
if err != nil {
t.Fatal(err)
}
})
t.Run("debug level", func(t *testing.T) {
var buf bytes.Buffer
lvl := &slog.LevelVar{}
lvl.Set(slog.LevelDebug)
h := 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(&buf)
for dec.ScanRecord() {
m := make(map[string]any)
for dec.ScanKeyval() {
m[string(dec.Key())] = dec.Value()
}
ms = append(ms, m)
}
if err := dec.Err(); err != nil {
t.Fatal(err)
}
return ms
}
err := slogtest.TestHandler(h, results)
if err != nil {
t.Fatal(err)
}
})
t.Run("dynamic level", func(t *testing.T) {
var buf bytes.Buffer
lvl := &slog.LevelVar{}
gklogger := log.NewLogfmtLogger(&buf)
h := 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
}