-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignore_handler_test.go
98 lines (83 loc) · 2.7 KB
/
ignore_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
package slogdedup
import (
"log/slog"
"strings"
"testing"
)
/*
{
"time": "2023-09-29T13:00:59Z",
"level": "WARN",
"msg": "main message",
"arg1": "with1arg1",
"arg2": "with1arg2",
"arg3": "with1arg3",
"arg4": "with2arg4",
"group1": "with2group1",
"level#01": "with2level",
"logging.googleapis.com/sourceLocation": "sourceLocationArg",
"message": "messageArg",
"message#01": "message#01Arg",
"msg#01": "prexisting01",
"msg#01a": "seekbug01a",
"msg#02": "seekbug02",
"severity": "severityArg",
"source#01": "with1source",
"sourceLoc": "sourceLocArg",
"time#01": "with1time",
"timestamp": "timestampArg",
"timestampRenamed": "timestampRenamedArg",
"typed": "overwritten",
"with1": "arg0",
"with2": "arg0"
}
*/
func TestIgnoreHandler(t *testing.T) {
t.Parallel()
tester := &testHandler{}
h := NewIgnoreHandler(tester, nil)
logComplex(t, h)
jBytes, err := tester.MarshalJSON()
if err != nil {
t.Errorf("Unable to marshal json: %v", err)
}
jStr := strings.TrimSpace(string(jBytes))
expected := `{"time":"2023-09-29T13:00:59Z","level":"WARN","msg":"main message","arg1":"with1arg1","arg2":"with1arg2","arg3":"with1arg3","arg4":"with2arg4","group1":"with2group1","level#01":"with2level","logging.googleapis.com/sourceLocation":"sourceLocationArg","message":"messageArg","message#01":"message#01Arg","msg#01":"prexisting01","msg#01a":"seekbug01a","msg#02":"seekbug02","severity":"severityArg","source#01":"with1source","sourceLoc":"sourceLocArg","time#01":"with1time","timestamp":"timestampArg","timestampRenamed":"timestampRenamedArg","typed":"overwritten","with1":"arg0","with2":"arg0"}`
if jStr != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, jStr)
}
// Uncomment to see the results
// t.Error(jStr)
// t.Error(tester.String())
checkRecordForDuplicates(t, tester.Record)
}
func TestIgnoreHandler_ResolveBuiltinKeyConflict(t *testing.T) {
t.Parallel()
tester := &testHandler{}
h := NewIgnoreMiddleware(&IgnoreHandlerOptions{
ResolveKey: func(groups []string, key string, _ int) (string, bool) {
if len(groups) > 0 {
return key, true
}
if key == "time" {
return "", false
} else {
return "arg-" + key, true
}
},
})(tester)
slog.New(h).Info("main message", "time", "hello", "foo", "bar")
jBytes, err := tester.MarshalJSON()
if err != nil {
t.Errorf("Unable to marshal json: %v", err)
}
jStr := strings.TrimSpace(string(jBytes))
expected := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","arg-foo":"bar"}`
if jStr != expected {
t.Errorf("Expected:\n%s\nGot:\n%s", expected, jStr)
}
// Uncomment to see the results
// t.Error(jStr)
// t.Error(tester.String())
checkRecordForDuplicates(t, tester.Record)
}