Skip to content

Commit

Permalink
Disable logging when all outputs are disabled (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr authored May 15, 2024
1 parent 2c4a658 commit c77acef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ func createLogOutput(cfg Config, enab zapcore.LevelEnabler) (zapcore.Core, error
case SystemdEnvironment, ContainerEnvironment:
return makeStderrOutput(cfg, enab)
case MacOSServiceEnvironment, WindowsServiceEnvironment:
fallthrough
default:
return makeFileOutput(cfg, enab)
default:
return zapcore.NewNopCore(), nil
}
}

Expand Down
19 changes: 19 additions & 0 deletions logp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,25 @@ func TestTypedLoggerCoreWith(t *testing.T) {
}
}

func TestCreateLogOutputAllDisabled(t *testing.T) {
cfg := DefaultConfig(DefaultEnvironment)
cfg.toIODiscard = false
cfg.toObserver = false
cfg.ToEventLog = false
cfg.ToFiles = false
cfg.ToStderr = false
cfg.ToSyslog = false

out, err := createLogOutput(cfg, zap.DebugLevel)
if err != nil {
t.Fatalf("did not expect an error calling createLogOutput: %s", err)
}

if out.Enabled(zap.DebugLevel) {
t.Fatal("the output must be disabled to all log levels")
}
}

func strField(key, val string) zapcore.Field {
return zapcore.Field{Type: zapcore.StringType, Key: key, String: val}
}
Expand Down

0 comments on commit c77acef

Please sign in to comment.