Skip to content

Commit c77acef

Browse files
authored
Disable logging when all outputs are disabled (#204)
1 parent 2c4a658 commit c77acef

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

logp/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ func createLogOutput(cfg Config, enab zapcore.LevelEnabler) (zapcore.Core, error
211211
case SystemdEnvironment, ContainerEnvironment:
212212
return makeStderrOutput(cfg, enab)
213213
case MacOSServiceEnvironment, WindowsServiceEnvironment:
214-
fallthrough
215-
default:
216214
return makeFileOutput(cfg, enab)
215+
default:
216+
return zapcore.NewNopCore(), nil
217217
}
218218
}
219219

logp/core_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,25 @@ func TestTypedLoggerCoreWith(t *testing.T) {
612612
}
613613
}
614614

615+
func TestCreateLogOutputAllDisabled(t *testing.T) {
616+
cfg := DefaultConfig(DefaultEnvironment)
617+
cfg.toIODiscard = false
618+
cfg.toObserver = false
619+
cfg.ToEventLog = false
620+
cfg.ToFiles = false
621+
cfg.ToStderr = false
622+
cfg.ToSyslog = false
623+
624+
out, err := createLogOutput(cfg, zap.DebugLevel)
625+
if err != nil {
626+
t.Fatalf("did not expect an error calling createLogOutput: %s", err)
627+
}
628+
629+
if out.Enabled(zap.DebugLevel) {
630+
t.Fatal("the output must be disabled to all log levels")
631+
}
632+
}
633+
615634
func strField(key, val string) zapcore.Field {
616635
return zapcore.Field{Type: zapcore.StringType, Key: key, String: val}
617636
}

0 commit comments

Comments
 (0)