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.
This commit is contained in:
Kévin Dunglas 2026-11-07 19:15:21 +01:00
parent c94f9e83ec
commit 37247c530a
No known key found for this signature in database
2 changed files with 56 additions and 9 deletions

View file

@ -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
}

View file

@ -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{})
}