Skip to content

Commit

Permalink
Cherry-pick #24466 to 7.12: Logging to file disabled on enroll (#24474)
Browse files Browse the repository at this point in the history
 Cherry-pick #24466 to 7.12: Logging to file disabled on enroll  (#24474)
  • Loading branch information
michalpristas committed Mar 15, 2021
1 parent 516198a commit 614c733
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- Fix failing installation on windows 7 {pull}[24387]24387
- Fix capabilities resolution in inspect command {pull}[24346]24346
- Fix windows installer during enroll {pull}[24343]24343
- Logging to file disabled on enroll {issue}[24173]24173

==== New features

Expand Down
5 changes: 5 additions & 0 deletions x-pack/elastic-agent/pkg/agent/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args
}
}

// enroll is invoked either manually or from install with redirected IO
// no need to log to file
cfg.Settings.LoggingConfig.ToFiles = false
cfg.Settings.LoggingConfig.ToStderr = true

logger, err := logger.NewFromConfig("", cfg.Settings.LoggingConfig)
if err != nil {
return err
Expand Down
16 changes: 12 additions & 4 deletions x-pack/elastic-agent/pkg/core/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ func new(name string, cfg *Config) (*Logger, error) {
if err != nil {
return nil, err
}
internal, err := makeInternalFileOutput(cfg)
if err != nil {
return nil, err

var outputs []zapcore.Core
if cfg.ToFiles {
internal, err := makeInternalFileOutput(cfg)
if err != nil {
return nil, err
}

outputs = append(outputs, internal)
}
if err := configure.LoggingWithOutputs("", commonCfg, internal); err != nil {

if err := configure.LoggingWithOutputs("", commonCfg, outputs...); err != nil {
return nil, fmt.Errorf("error initializing logging: %v", err)
}
return logp.NewLogger(name), nil
Expand Down Expand Up @@ -87,6 +94,7 @@ func DefaultLoggingConfig() *Config {
cfg := logp.DefaultConfig(logp.DefaultEnvironment)
cfg.Beat = agentName
cfg.Level = logp.InfoLevel
cfg.ToFiles = true
cfg.Files.Path = paths.Logs()
cfg.Files.Name = fmt.Sprintf("%s.log", agentName)

Expand Down

0 comments on commit 614c733

Please sign in to comment.