diff --git a/libbeat/logp/config.go b/libbeat/logp/config.go index d1c2e01c9cb0..6dbfeddfb2c6 100644 --- a/libbeat/logp/config.go +++ b/libbeat/logp/config.go @@ -38,6 +38,7 @@ type Config struct { Files FileConfig `config:"files"` + environment Environment addCaller bool // Adds package and line number info to messages. development bool // Controls how DPanic behaves. } @@ -59,41 +60,16 @@ const defaultLevel = InfoLevel // DefaultConfig returns the default config options for a given environment the // Beat is supposed to be run within. func DefaultConfig(environment Environment) Config { - switch environment { - case SystemdEnvironment, ContainerEnvironment: - return defaultToStderrConfig() - - case MacOSServiceEnvironment, WindowsServiceEnvironment: - fallthrough - default: - return defaultToFileConfig() - } -} - -func defaultToStderrConfig() Config { return Config{ - Level: defaultLevel, - ToStderr: true, - Files: defaultFileConfig(), - addCaller: true, - } -} - -func defaultToFileConfig() Config { - return Config{ - Level: defaultLevel, - ToFiles: true, - Files: defaultFileConfig(), - addCaller: true, - } -} - -func defaultFileConfig() FileConfig { - return FileConfig{ - MaxSize: 10 * 1024 * 1024, - MaxBackups: 7, - Permissions: 0600, - Interval: 0, - RotateOnStartup: true, + Level: defaultLevel, + Files: FileConfig{ + MaxSize: 10 * 1024 * 1024, + MaxBackups: 7, + Permissions: 0600, + Interval: 0, + RotateOnStartup: true, + }, + environment: environment, + addCaller: true, } } diff --git a/libbeat/logp/core.go b/libbeat/logp/core.go index b0204a62ac76..e27768e07cd4 100644 --- a/libbeat/logp/core.go +++ b/libbeat/logp/core.go @@ -67,21 +67,10 @@ func Configure(cfg Config) error { ) // Build a single output (stderr has priority if more than one are enabled). - switch { - case cfg.toObserver: + if cfg.toObserver { sink, observedLogs = observer.New(cfg.Level.zapLevel()) - case cfg.toIODiscard: - sink, err = makeDiscardOutput(cfg) - case cfg.ToStderr: - sink, err = makeStderrOutput(cfg) - case cfg.ToSyslog: - sink, err = makeSyslogOutput(cfg) - case cfg.ToEventLog: - sink, err = makeEventLogOutput(cfg) - case cfg.ToFiles: - fallthrough - default: - sink, err = makeFileOutput(cfg) + } else { + sink, err = createLogOutput(cfg) } if err != nil { return errors.Wrap(err, "failed to build log output") @@ -125,6 +114,30 @@ func Configure(cfg Config) error { return nil } +func createLogOutput(cfg Config) (zapcore.Core, error) { + switch { + case cfg.toIODiscard: + return makeDiscardOutput(cfg) + case cfg.ToStderr: + return makeStderrOutput(cfg) + case cfg.ToSyslog: + return makeSyslogOutput(cfg) + case cfg.ToEventLog: + return makeEventLogOutput(cfg) + case cfg.ToFiles: + return makeFileOutput(cfg) + } + + switch cfg.environment { + case SystemdEnvironment, ContainerEnvironment: + return makeStderrOutput(cfg) + case MacOSServiceEnvironment, WindowsServiceEnvironment: + fallthrough + default: + return makeFileOutput(cfg) + } +} + // DevelopmentSetup configures the logger in development mode at debug level. // By default the output goes to stderr. func DevelopmentSetup(options ...Option) error {