Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VC-35738] Log with klog to stdout and stderr in Kubernetes text format #596

Merged
merged 12 commits into from
Oct 31, 2024
Merged
Prev Previous commit
Next Next commit
logs_test.go: replaceWithStaticTimestamps breaks for short PIDs
  • Loading branch information
maelvls authored and wallrj committed Oct 29, 2024
commit d9bc223c8ae205f20bdc3d3e4f22d011268226a2
21 changes: 20 additions & 1 deletion pkg/logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ E0000 00:00:00.000000 00000 logs_test.go:000] "Contextual error" err="fake-err

var (
timestampRegexpStdLog = regexp.MustCompile(`\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}`)
timestampRegexpKlog = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6} \d{5}`)
timestampRegexpKlog = regexp.MustCompile(`\d{4} \d{2}:\d{2}:\d{2}\.\d{6} +\d+`)
timestampRegexpJSON = regexp.MustCompile(`"ts":\d+\.\d+`)
fileAndLineRegexpJSON = regexp.MustCompile(`"caller":"([^"]+).go:\d+"`)
fileAndLineRegexpKlog = regexp.MustCompile(` ([^:]+).go:\d+`)
Expand Down Expand Up @@ -318,6 +318,25 @@ func replaceWithStaticTimestamps(input string) string {
return input
}

func Test_replaceWithStaticTimestamps(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "klog",
input: `I1018 15:20:42.861239 2386 logs_test.go:13] "Contextual Info Level 3" logger="foo" key="value"`,
expected: `I0000 00:00:00.000000 00000 logs_test.go:000] "Contextual Info Level 3" logger="foo" key="value"`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, replaceWithStaticTimestamps(test.input))
})
}
}

func TestLogToSlogWriter(t *testing.T) {
// This test makes sure that all the agent's Log.Fatalf calls are correctly
// translated to slog.Error calls.
Expand Down