From 37247c530a02a4d86e7c60387f0f8a5fbfef0bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 7 Nov 2026 19:15:21 +0100 Subject: [PATCH] logging: keep discard logs that have a core Custom logs writing to discard were deleted, so a core module such as an exporter never received their entries. --- logging.go | 18 +++++++++--------- logging_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/logging.go b/logging.go index beddf9e70..d00768d09 100644 --- a/logging.go +++ b/logging.go @@ -124,15 +124,15 @@ func (logging *Logging) openLogs(ctx Context) error { return fmt.Errorf("setting up custom log '%s': %v", name, err) } - // Any other logs that use the discard writer can be deleted - // entirely. This avoids encoding and processing of each - // log entry that would just be thrown away anyway. Notably, - // we do not reach this point for the default log, which MUST - // exist, otherwise core log emissions would panic because - // they use the Log() function directly which expects a non-nil - // logger. Even if we keep logs with a discard writer, they - // have a nop core, and keeping them at all seems unnecessary. - if _, ok := l.writerOpener.(*DiscardWriter); ok { + // Any other logs that use the discard writer and no core + // module can be deleted entirely. This avoids encoding and + // processing of each log entry that would just be thrown + // away anyway. Notably, we do not reach this point for the + // default log, which MUST exist, otherwise core log emissions + // would panic because they use the Log() function directly + // which expects a non-nil logger. Even if we keep such logs, + // they have a nop core, and keeping them at all seems unnecessary. + if l.core == zapcore.NewNopCore() { delete(logging.Logs, name) continue } diff --git a/logging_test.go b/logging_test.go index 04891c233..20e6c0811 100644 --- a/logging_test.go +++ b/logging_test.go @@ -16,6 +16,8 @@ package caddy import ( "bytes" + "context" + "encoding/json" "strings" "testing" "time" @@ -229,3 +231,48 @@ type nopWriteCloser struct{} func (nopWriteCloser) Write(p []byte) (int, error) { return len(p), nil } func (nopWriteCloser) Close() error { return nil } + +func TestOpenLogsKeepsDiscardLogsWithCoreModule(t *testing.T) { + defaultLoggerMu.RLock() + origDefault := defaultLogger + defaultLoggerMu.RUnlock() + t.Cleanup(func() { + defaultLoggerMu.Lock() + defaultLogger = origDefault + defaultLoggerMu.Unlock() + }) + + ctx, cancel := NewContext(Context{Context: context.Background()}) + defer cancel() + + discard := json.RawMessage(`{"output":"discard"}`) + logging := &Logging{Logs: map[string]*CustomLog{ + "discarded": {BaseLog: BaseLog{WriterRaw: discard}}, + "exported": {BaseLog: BaseLog{WriterRaw: discard, CoreRaw: json.RawMessage(`{"module":"test"}`)}}, + }} + if err := logging.openLogs(ctx); err != nil { + t.Fatal(err) + } + + if _, ok := logging.Logs["discarded"]; ok { + t.Error("expected the discard log without a core module to be deleted") + } + if _, ok := logging.Logs["exported"]; !ok { + t.Error("expected the discard log with a core module to be kept") + } +} + +type testCore struct { + zapcore.Core `json:"-"` +} + +func (testCore) CaddyModule() ModuleInfo { + return ModuleInfo{ + ID: "caddy.logging.cores.test", + New: func() Module { return &testCore{Core: zapcore.NewNopCore()} }, + } +} + +func init() { + RegisterModule(testCore{}) +}