-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverwrite_handler_test.go
124 lines (109 loc) · 3.8 KB
/
overwrite_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
package slogdedup
import (
"log/slog"
"strings"
"testing"
)
/*
{
"time": "2023-09-29T13:00:59Z",
"level": "WARN",
"msg": "main message",
"arg1": "with2arg1",
"arg2": "with1arg2",
"arg3": "with2arg3",
"arg4": "with2arg4",
"group1": {
"arg1": "main1arg1",
"arg2": "group1with3arg2",
"arg3": "group1with4arg3",
"arg4": "group1with4arg4",
"arg5": "with4inlinedGroupArg5",
"arg6": "main1arg6",
"level": "main1level",
"main1": "arg0",
"main1group3": {
"group3": "group3arg0"
},
"msg": "with4msg",
"overwrittenGroup": "with4overwrittenGroup",
"separateGroup2": {
"arg1": "group2arg1",
"arg2": "group2arg2",
"group2": "group2arg0"
},
"source": "with3source",
"time": "with3time",
"with3": "arg0",
"with4": "arg0"
},
"level#01": {
"inlinedLevelGroupKey": "inlinedLevelGroupValue"
},
"logging.googleapis.com/sourceLocation": "sourceLocationArg",
"message": "messageArg",
"message#01": "message#01Arg",
"msg#01": "with2msg2",
"msg#01a": "seekbug01a",
"msg#02": "seekbug02",
"severity": "severityArg",
"source#01": "with1source",
"sourceLoc": "sourceLocArg",
"time#01": "with1time",
"timestamp": "timestampArg",
"timestampRenamed": "timestampRenamedArg",
"typed": true,
"with1": "arg0",
"with2": "arg0"
}
*/
func TestOverwriteHandler(t *testing.T) {
t.Parallel()
tester := &testHandler{}
h := NewOverwriteHandler(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":"with2arg1","arg2":"with1arg2","arg3":"with2arg3","arg4":"with2arg4","group1":{"arg1":"main1arg1","arg2":"group1with3arg2","arg3":"group1with4arg3","arg4":"group1with4arg4","arg5":"with4inlinedGroupArg5","arg6":"main1arg6","level":"main1level","main1":"arg0","main1group3":{"group3":"group3arg0"},"msg":"with4msg","overwrittenGroup":"with4overwrittenGroup","separateGroup2":{"arg1":"group2arg1","arg2":"group2arg2","group2":"group2arg0"},"source":"with3source","time":"with3time","with3":"arg0","with4":"arg0"},"level#01":{"inlinedLevelGroupKey":"inlinedLevelGroupValue"},"logging.googleapis.com/sourceLocation":"sourceLocationArg","message":"messageArg","message#01":"message#01Arg","msg#01":"with2msg2","msg#01a":"seekbug01a","msg#02":"seekbug02","severity":"severityArg","source#01":"with1source","sourceLoc":"sourceLocArg","time#01":"with1time","timestamp":"timestampArg","timestampRenamed":"timestampRenamedArg","typed":true,"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)
}
/*
{
"time": "2023-09-29T13:00:59Z",
"level": "INFO",
"msg": "case insenstive, drop builtin conflict",
"ARG1": "val2"
}
*/
func TestOverwriteHandler_CaseInsensitiveDropBuiltinConflicts(t *testing.T) {
t.Parallel()
tester := &testHandler{}
h := NewOverwriteMiddleware(&OverwriteHandlerOptions{
KeyCompare: CaseInsensitiveCmp,
ResolveKey: DropIfBuiltinKeyConflict,
})(tester)
log := slog.New(h)
log.Info("case insenstive, drop builtin conflict", "arg1", "val1", "ARG1", "val2", slog.MessageKey, "builtin-conflict")
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":"case insenstive, drop builtin conflict","ARG1":"val2"}`
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)
}