-
Notifications
You must be signed in to change notification settings - Fork 0
/
append_handler_test.go
165 lines (151 loc) · 4.58 KB
/
append_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
package slogdedup
import (
"log/slog"
"strings"
"testing"
)
/*
{
"time": "2023-09-29T13:00:59Z",
"level": "WARN",
"msg": "main message",
"arg1": [
"with1arg1",
"with2arg1"
],
"arg2": "with1arg2",
"arg3": [
"with1arg3",
"with2arg3"
],
"arg4": "with2arg4",
"group1": [
"with2group1",
{
"arg1": [
"group1with3arg1",
"group1with4arg1",
"main1arg1"
],
"arg2": "group1with3arg2",
"arg3": [
"group1with3arg3",
"group1with4arg3"
],
"arg4": "group1with4arg4",
"arg5": "with4inlinedGroupArg5",
"arg6": "main1arg6",
"level": [
"with4overwritten",
"main1overwritten",
"main1level"
],
"main1": "arg0",
"main1group3": {
"group3": [
"group3overwritten",
"group3arg0"
]
},
"msg": "with4msg",
"overwrittenGroup": [
{
"arg": "arg"
},
"with4overwrittenGroup"
],
"separateGroup2": {
"arg1": "group2arg1",
"arg2": "group2arg2",
"group2": "group2arg0"
},
"source": "with3source",
"time": "with3time",
"with3": "arg0",
"with4": "arg0"
}
],
"level#01": [
"with2level",
{
"levelGroupKey": "levelGroupValue"
},
{
"inlinedLevelGroupKey": "inlinedLevelGroupValue"
}
],
"logging.googleapis.com/sourceLocation": "sourceLocationArg",
"message": "messageArg",
"message#01": "message#01Arg",
"msg#01": [
"prexisting01",
"with2msg",
"with2msg2"
],
"msg#01a": "seekbug01a",
"msg#02": "seekbug02",
"severity": "severityArg",
"source#01": "with1source",
"sourceLoc": "sourceLocArg",
"time#01": "with1time",
"timestamp": "timestampArg",
"timestampRenamed": "timestampRenamedArg",
"typed": [
"overwritten",
3,
true
],
"with1": "arg0",
"with2": "arg0"
}
*/
func TestAppendHandler(t *testing.T) {
t.Parallel()
tester := &testHandler{}
h := NewAppendHandler(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","with2arg1"],"arg2":"with1arg2","arg3":["with1arg3","with2arg3"],"arg4":"with2arg4","group1":["with2group1",{"arg1":["group1with3arg1","group1with4arg1","main1arg1"],"arg2":"group1with3arg2","arg3":["group1with3arg3","group1with4arg3"],"arg4":"group1with4arg4","arg5":"with4inlinedGroupArg5","arg6":"main1arg6","level":["with4overwritten","main1overwritten","main1level"],"main1":"arg0","main1group3":{"group3":["group3overwritten","group3arg0"]},"msg":"with4msg","overwrittenGroup":[{"arg":"arg"},"with4overwrittenGroup"],"separateGroup2":{"arg1":"group2arg1","arg2":"group2arg2","group2":"group2arg0"},"source":"with3source","time":"with3time","with3":"arg0","with4":"arg0"}],"level#01":["with2level",{"levelGroupKey":"levelGroupValue"},{"inlinedLevelGroupKey":"inlinedLevelGroupValue"}],"logging.googleapis.com/sourceLocation":"sourceLocationArg","message":"messageArg","message#01":"message#01Arg","msg#01":["prexisting01","with2msg","with2msg2"],"msg#01a":"seekbug01a","msg#02":"seekbug02","severity":"severityArg","source#01":"with1source","sourceLoc":"sourceLocArg","time#01":"with1time","timestamp":"timestampArg","timestampRenamed":"timestampRenamedArg","typed":["overwritten",3,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, keep builtin conflict",
"arg1": ["val1","val2"],
"msg":"builtin-conflict"
}
*/
func TestAppendHandler_CaseInsensitiveKeepIfBuiltinConflict(t *testing.T) {
t.Parallel()
tester := &testHandler{}
h := NewAppendMiddleware(&AppendHandlerOptions{
KeyCompare: CaseInsensitiveCmp,
ResolveKey: KeepIfBuiltinKeyConflict,
})(tester)
log := slog.New(h)
log.Info("case insenstive, keep 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, keep builtin conflict","arg1":["val1","val2"],"msg":"builtin-conflict"}`
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())
}