Skip to content

Commit

Permalink
[logp] Allow a logger to change its output
Browse files Browse the repository at this point in the history
This commit enables a logger to change its output after creation and
independently from the global logger configuration.
  • Loading branch information
belimawr committed Dec 21, 2023
1 parent b200fb1 commit aedb70a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions logp/configure/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func init() {
flag.Var((*environmentVar)(&environment), "environment", "set environment being ran in")
}

func GetEnvironment() logp.Environment {
return environment
}

// Logging builds a logp.Config based on the given common.Config and the specified
// CLI flags.
func Logging(beatName string, cfg *config.C) error {
Expand Down
16 changes: 16 additions & 0 deletions logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ func makeEventLogOutput(cfg Config, enab zapcore.LevelEnabler) (zapcore.Core, er
return wrappedCore(core), nil
}

// WithFileOutput creates a new file output based on cfg and
// replaces the previous one.
func WithFileOutput(cfg Config) func(zapcore.Core) zapcore.Core {
out, err := makeFileOutput(cfg, zap.DebugLevel)
if err != nil {
L().Errorf("could not create file output: %s", err)
out = zapcore.NewNopCore()
}

f := func(zapcore.Core) zapcore.Core {
return out
}

return f
}

func makeFileOutput(cfg Config, enab zapcore.LevelEnabler) (zapcore.Core, error) {
filename := paths.Resolve(paths.Logs, filepath.Join(cfg.Files.Path, cfg.LogFilename()))

Expand Down

0 comments on commit aedb70a

Please sign in to comment.