Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postpone selection of the logger output #17448

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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