Skip to content

Commit

Permalink
Cherry-pick #17448 to 7.x: Postpone selection of the logger ou… (#17454)
Browse files Browse the repository at this point in the history
(cherry picked from commit d6c2aac)
  • Loading branch information
Steffen Siering authored Apr 3, 2020
1 parent dfae91c commit 2d94c55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
46 changes: 11 additions & 35 deletions libbeat/logp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand All @@ -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,
}
}
41 changes: 27 additions & 14 deletions libbeat/logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2d94c55

Please sign in to comment.