From 0c7c91a447922022163bbcc107512bc0b7f8a48b Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Tue, 14 Apr 2026 09:33:02 +1000 Subject: [PATCH] logging: preserve ts for journald-wrapped JSON logs (#7644) --- modules/logging/journaldencoder.go | 15 ++++++--------- modules/logging/journaldencoder_test.go | 6 +++--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/modules/logging/journaldencoder.go b/modules/logging/journaldencoder.go index 5826c6950..142e99335 100644 --- a/modules/logging/journaldencoder.go +++ b/modules/logging/journaldencoder.go @@ -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) } } diff --git a/modules/logging/journaldencoder_test.go b/modules/logging/journaldencoder_test.go index 7b9a17d77..8676140dd 100644 --- a/modules/logging/journaldencoder_test.go +++ b/modules/logging/journaldencoder_test.go @@ -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) } }