logging: preserve ts for journald-wrapped JSON logs (#7644)

This commit is contained in:
Zen Dodd 2026-04-14 09:33:02 +10:00 committed by GitHub
parent 1a3e900b35
commit 0c7c91a447
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 12 deletions

View file

@ -81,7 +81,7 @@ func (je *JournaldEncoder) Provision(ctx caddy.Context) error {
je.Encoder = val.(zapcore.Encoder)
}
suppressEncoderTimestamp(je.Encoder)
suppressConsoleEncoderTimestamp(je.Encoder)
return nil
}
@ -106,7 +106,7 @@ func (je *JournaldEncoder) ConfigureDefaultFormat(wo caddy.WriterOpener) error {
}
}
suppressEncoderTimestamp(je.Encoder)
suppressConsoleEncoderTimestamp(je.Encoder)
return nil
}
@ -194,22 +194,19 @@ func journaldPriorityPrefix(level zapcore.Level) string {
}
}
func suppressEncoderTimestamp(enc zapcore.Encoder) {
func suppressConsoleEncoderTimestamp(enc zapcore.Encoder) {
empty := ""
switch e := enc.(type) {
case *ConsoleEncoder:
e.TimeKey = &empty
_ = e.Provision(caddy.Context{})
case *JSONEncoder:
e.TimeKey = &empty
_ = e.Provision(caddy.Context{})
case *AppendEncoder:
suppressEncoderTimestamp(e.wrapped)
suppressConsoleEncoderTimestamp(e.wrapped)
case *FilterEncoder:
suppressEncoderTimestamp(e.wrapped)
suppressConsoleEncoderTimestamp(e.wrapped)
case *JournaldEncoder:
suppressEncoderTimestamp(e.Encoder)
suppressConsoleEncoderTimestamp(e.Encoder)
}
}

View file

@ -89,7 +89,7 @@ journald {
}
}
func TestJournaldEncoderSuppressesJSONTimestamp(t *testing.T) {
func TestJournaldEncoderPreservesJSONTimestamp(t *testing.T) {
enc := &JournaldEncoder{
Encoder: &JSONEncoder{},
}
@ -108,8 +108,8 @@ func TestJournaldEncoderSuppressesJSONTimestamp(t *testing.T) {
defer buf.Free()
got := buf.String()
if strings.Contains(got, `"ts"`) {
t.Fatalf("got JSON output with ts field: %q", got)
if !strings.Contains(got, `"ts"`) {
t.Fatalf("got JSON output without ts field: %q", got)
}
}