-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_output_test.go
More file actions
374 lines (304 loc) · 12.1 KB
/
log_output_test.go
File metadata and controls
374 lines (304 loc) · 12.1 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package main
import (
"encoding/json"
"errors"
"net"
"testing"
"time"
"github.com/Shimmur/logtailer/reporter"
. "github.com/smartystreets/goconvey/convey"
)
func Test_extractLogLevelWithRegex(t *testing.T) {
Convey("extractLogLevelWithRegex()", t, func() {
Convey("extracts info level from logfmt", func() {
line := `time="2025-11-14T09:02:08Z" level=info msg="Started Worker" Namespace=workflow-automation`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatGo))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "info")
})
Convey("extracts error level from logfmt", func() {
line := `time="2025-11-14T09:02:08Z" level=error msg="Something failed"`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatGo))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "error")
})
Convey("extracts warning level from logfmt", func() {
line := `time="2025-11-14T09:36:09Z" level=warning msg="harvest failure" cmd=metric_data`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatGo))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "warning")
})
Convey("returns false when no level found", func() {
line := `This is just a plain text log with no level`
_, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatGo))
So(found, ShouldBeFalse)
})
Convey("extracts info level from JSON format", func() {
line := `{"level":"info","ts":"2025-12-18T07:15:30Z","logger":"controllers.ingress","msg":"successfully deployed model","ingressGroup":"k8s-internal-dev"}`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatJSON))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "info")
})
Convey("extracts error level from JSON format", func() {
line := `{"level":"error","ts":"2025-12-18T07:15:30Z","logger":"controllers.ingress","msg":"deployment failed"}`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatJSON))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "error")
})
Convey("extracts warn level from JSON format", func() {
line := `{"level":"warn","ts":"2025-12-18T07:15:30Z","logger":"controllers.ingress","msg":"deprecated API used"}`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatJSON))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "warn")
})
Convey("extracts info level from tab-separated format", func() {
line := `2025-12-18T06:20:47.312Z INFO main Bootstrap memory-manager.http-client.log.path`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatTab))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "info")
})
Convey("extracts error level from tab-separated format", func() {
line := `2025-12-18T06:20:47.312Z ERROR main Bootstrap Failed to initialize`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatTab))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "error")
})
Convey("extracts warning level from tab-separated format", func() {
line := `2025-12-18T06:20:47.312Z WARN main Bootstrap Deprecated configuration`
level, found := extractLogLevelWithRegex(line, selectLogRegex(LogFormatTab))
So(found, ShouldBeTrue)
So(level, ShouldEqual, "warn")
})
})
}
func Test_selectLogRegex_ValidFormats(t *testing.T) {
Convey("selectLogRegex() with valid formats", t, func() {
Convey("LogFormatGo returns go-logfmt format", func() {
regex := selectLogRegex(LogFormatGo)
So(regex, ShouldEqual, logFormatRegexes[LogFormatGo])
// Verify it actually works by testing with a logfmt line
line := `level=info msg="test"`
level, found := extractLogLevelWithRegex(line, regex)
So(found, ShouldBeTrue)
So(level, ShouldEqual, "info")
})
Convey("LogFormatJSON returns json format", func() {
regex := selectLogRegex(LogFormatJSON)
So(regex, ShouldEqual, logFormatRegexes[LogFormatJSON])
// Verify it actually works
line := `{"level":"warn","msg":"test"}`
level, found := extractLogLevelWithRegex(line, regex)
So(found, ShouldBeTrue)
So(level, ShouldEqual, "warn")
})
Convey("LogFormatTab returns tab-separated format", func() {
regex := selectLogRegex(LogFormatTab)
So(regex, ShouldEqual, logFormatRegexes[LogFormatTab])
// Verify it actually works
line := `2025-12-18T06:20:47.312Z ERROR main test`
level, found := extractLogLevelWithRegex(line, regex)
So(found, ShouldBeTrue)
So(level, ShouldEqual, "error")
})
})
}
func Test_parseLogFormat(t *testing.T) {
Convey("parseLogFormat()", t, func() {
Convey("empty string returns LogFormatGo", func() {
So(parseLogFormat(""), ShouldEqual, LogFormatGo)
})
Convey("go-logfmt returns LogFormatGo", func() {
So(parseLogFormat("go-logfmt"), ShouldEqual, LogFormatGo)
})
Convey("json returns LogFormatJSON", func() {
So(parseLogFormat("json"), ShouldEqual, LogFormatJSON)
})
Convey("tab returns LogFormatTab", func() {
So(parseLogFormat("tab"), ShouldEqual, LogFormatTab)
})
Convey("ruby returns LogFormatRuby", func() {
So(parseLogFormat("ruby"), ShouldEqual, LogFormatRuby)
})
Convey("invalid format logs warning and falls back to LogFormatGo", func() {
output := LogCapture(func() {
format := parseLogFormat("xml")
So(format, ShouldEqual, LogFormatGo)
})
So(output, ShouldContainSubstring, "Unsupported log format 'xml'")
So(output, ShouldContainSubstring, "falling back to 'go-logfmt'")
})
Convey("wrong case format logs warning and falls back", func() {
output := LogCapture(func() {
format := parseLogFormat("JSON")
So(format, ShouldEqual, LogFormatGo)
})
So(output, ShouldContainSubstring, "Unsupported log format 'JSON'")
So(output, ShouldContainSubstring, "falling back to 'go-logfmt'")
})
})
}
func Test_UDPSyslogger(t *testing.T) {
theJson := struct {
Environment string `json:"Environment"`
Level string `json:"Level"`
Payload string `json:"Payload"`
ServiceName string `json:"ServiceName"`
Timestamp time.Time `json:"Timestamp"`
Container string `json:"Container"`
LogFormat string `json:"LogFormat"`
}{}
Convey("UDPSyslogger()", t, func() {
Convey("works end-to-end", func() {
enableRegexLogLevelParsing := false
logger := NewUDPSyslogger(map[string]string{
"ServiceName": "bocaccio",
"Environment": "medieval",
}, "127.0.0.1:9714", enableRegexLogLevelParsing, "") // enhanced regex parsing disabled to test og mode
logLine := "2022-12-06T12:20:28.418060579Z stdout F this is a test log line 💵 with UTF-8"
go func() {
logger.Log(&LogLine{Text: logLine, Container: "beowulf"})
}()
received, err := ListenUDP("127.0.0.1:9714")
So(err, ShouldBeNil)
So(received, ShouldNotBeEmpty)
err = json.Unmarshal(received, &theJson)
So(err, ShouldBeNil)
So(theJson.Environment, ShouldEqual, "medieval")
So(theJson.ServiceName, ShouldEqual, "bocaccio")
So(theJson.Payload, ShouldEqual, logLine[40:len(logLine)])
So(theJson.Timestamp, ShouldNotBeEmpty)
So(theJson.Container, ShouldEqual, "beowulf")
So(theJson.LogFormat, ShouldEqual, "") // Should be empty when regex parsing disabled
})
Convey("correctly parses level from structured logs on stderr", func() {
enableRegexLogLevelParsing := true
logger := NewUDPSyslogger(map[string]string{
"ServiceName": "service",
"Environment": "prod",
}, "127.0.0.1:9715", enableRegexLogLevelParsing, "")
// Info level info on stderr - should be logged as Info, not Error
infoLog := `2025-11-14T09:02:08.322480471Z stderr F time="2025-11-14T09:02:08Z" level=info msg="Started Worker" Namespace=default`
go func() {
logger.Log(&LogLine{Text: infoLog, Container: "worker"})
}()
received, err := ListenUDP("127.0.0.1:9715")
So(err, ShouldBeNil)
So(received, ShouldNotBeEmpty)
err = json.Unmarshal(received, &theJson)
So(err, ShouldBeNil)
// Should be logged as "info", NOT "error" despite being on stderr
So(theJson.Level, ShouldEqual, "info")
So(theJson.LogFormat, ShouldEqual, "go-logfmt") // Should default to go-logfmt when empty
})
Convey("correctly parses warning level from structured logs", func() {
enableRegexLogLevelParsing := true
logger := NewUDPSyslogger(map[string]string{
"ServiceName": "service",
"Environment": "prod",
}, "127.0.0.1:9716", enableRegexLogLevelParsing, "")
// Warning level log on stderr
warnLog := `2025-11-14T09:36:09.227628554Z stderr F time="2025-11-14T09:36:09Z" level=warning msg="harvest failure" cmd=metric_data component=newrelic`
go func() {
logger.Log(&LogLine{Text: warnLog, Container: "worker"})
}()
received, err := ListenUDP("127.0.0.1:9716")
So(err, ShouldBeNil)
So(received, ShouldNotBeEmpty)
err = json.Unmarshal(received, &theJson)
So(err, ShouldBeNil)
So(theJson.Level, ShouldEqual, "warning")
})
Convey("correctly handles error level logs", func() {
enableRegexLogLevelParsing := true
logger := NewUDPSyslogger(map[string]string{
"ServiceName": "service",
"Environment": "prod",
}, "127.0.0.1:9717", enableRegexLogLevelParsing, "")
// Error level log
errorLog := `2025-11-14T09:02:08.322480471Z stderr F time="2025-11-14T09:02:08Z" level=error msg="Connection failed" error="timeout"`
go func() {
logger.Log(&LogLine{Text: errorLog, Container: "worker"})
}()
received, err := ListenUDP("127.0.0.1:9717")
So(err, ShouldBeNil)
So(received, ShouldNotBeEmpty)
err = json.Unmarshal(received, &theJson)
So(err, ShouldBeNil)
// Should be logged as "error"
So(theJson.Level, ShouldEqual, "error")
})
Convey("correctly handles quoted error level logs", func() {
enableRegexLogLevelParsing := true
logger := NewUDPSyslogger(map[string]string{
"ServiceName": "service",
"Environment": "prod",
}, "127.0.0.1:9717", enableRegexLogLevelParsing, "")
// Error level log
errorLog := `2025-11-14T09:02:08.322480471Z stderr F time="2025-11-14T09:02:08Z" level="error" msg="Connection failed" error="timeout"`
go func() {
logger.Log(&LogLine{Text: errorLog, Container: "worker"})
}()
received, err := ListenUDP("127.0.0.1:9717")
So(err, ShouldBeNil)
So(received, ShouldNotBeEmpty)
err = json.Unmarshal(received, &theJson)
So(err, ShouldBeNil)
// Should be logged as "error"
So(theJson.Level, ShouldEqual, "error")
})
Convey("correctly handles unknown level logs as info", func() {
enableRegexLogLevelParsing := true
logger := NewUDPSyslogger(map[string]string{
"ServiceName": "service",
"Environment": "prod",
}, "127.0.0.1:9717", enableRegexLogLevelParsing, "")
// Error level log
errorLog := `2025-11-14T09:02:08.322480471Z stderr F time="2025-11-14T09:02:08Z" level=unknown msg="Connection failed" error="timeout"`
go func() {
logger.Log(&LogLine{Text: errorLog, Container: "worker"})
}()
received, err := ListenUDP("127.0.0.1:9717")
So(err, ShouldBeNil)
So(received, ShouldNotBeEmpty)
err = json.Unmarshal(received, &theJson)
So(err, ShouldBeNil)
// Should be logged as "error"
So(theJson.Level, ShouldEqual, "info")
})
})
}
func Test_RateLimitingLogger(t *testing.T) {
Convey("RateLimitingLogger", t, func() {
rptr := reporter.NewLimitExceededReporter("", "", "")
mockUpstream := &mockLogOutput{}
logger := NewRateLimitingLogger(
rptr, 1, 1*time.Millisecond, "ServiceName", mockUpstream,
)
Convey("can detect when logging has gone too far", func() {
logger.Log(&LogLine{Text: "a line"})
So(mockUpstream.WasCalled, ShouldBeTrue)
mockUpstream.WasCalled = false
logger.Log(&LogLine{Text: "a line 2"})
So(mockUpstream.WasCalled, ShouldBeFalse)
logger.Log(&LogLine{Text: "a line 3"})
So(mockUpstream.WasCalled, ShouldBeFalse)
So(mockUpstream.LastLogged, ShouldResemble, &LogLine{Text: "a line"})
})
})
}
func ListenUDP(address string) ([]byte, error) {
pc, err := net.ListenPacket("udp", address)
if err != nil {
return nil, err
}
defer pc.Close()
buf := make([]byte, 1024)
n, _, err := pc.ReadFrom(buf)
if err != nil {
return nil, err
}
if n < 1 {
return nil, errors.New("received nothing")
}
return buf[:n], nil
}