From c67e0d404cc7f34d787f4e2ade92d0fd2cdeb902 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 16 Jun 2021 16:09:35 +0200 Subject: [PATCH] [7.x](backport #25998) Add more ECS fields to logs (#26266) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit bfbadaa59553a27549eec851beaefe2c7df5c397) Co-authored-by: Juan Álvarez --- libbeat/logp/config.go | 11 +++++++++++ libbeat/logp/core.go | 13 ++++++++----- libbeat/logp/core_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/libbeat/logp/config.go b/libbeat/logp/config.go index d6495cb985a..19957e2bdc5 100644 --- a/libbeat/logp/config.go +++ b/libbeat/logp/config.go @@ -80,3 +80,14 @@ func DefaultConfig(environment Environment) Config { addCaller: true, } } + +// LogFilename returns the base filename to which logs will be written for +// the "files" log output. If another log output is used, or `logging.files.name` +// is unspecified, then the beat name will be returned. +func (cfg Config) LogFilename() string { + name := cfg.Beat + if cfg.Files.Name != "" { + name = cfg.Files.Name + } + return name +} diff --git a/libbeat/logp/core.go b/libbeat/logp/core.go index f4ccb2eece2..0bcf76f5edc 100644 --- a/libbeat/logp/core.go +++ b/libbeat/logp/core.go @@ -196,6 +196,13 @@ func makeOptions(cfg Config) []zap.Option { if cfg.development { options = append(options, zap.Development()) } + if cfg.ECSEnabled { + fields := []zap.Field{ + zap.String("service.name", cfg.Beat), + zap.String("event.dataset", cfg.LogFilename()), + } + options = append(options, zap.Fields(fields...)) + } return options } @@ -226,11 +233,7 @@ func makeEventLogOutput(cfg Config) (zapcore.Core, error) { } func makeFileOutput(cfg Config) (zapcore.Core, error) { - name := cfg.Beat - if cfg.Files.Name != "" { - name = cfg.Files.Name - } - filename := paths.Resolve(paths.Logs, filepath.Join(cfg.Files.Path, name)) + filename := paths.Resolve(paths.Logs, filepath.Join(cfg.Files.Path, cfg.LogFilename())) rotator, err := file.NewFileRotator(filename, file.MaxSizeBytes(cfg.Files.MaxSize), diff --git a/libbeat/logp/core_test.go b/libbeat/logp/core_test.go index 9ebe0bb38de..760bfe13918 100644 --- a/libbeat/logp/core_test.go +++ b/libbeat/logp/core_test.go @@ -146,3 +146,30 @@ func TestNotDebugAllStdoutDisablesDefaultGoLogger(t *testing.T) { DevelopmentSetup(WithSelectors("other"), WithLevel(InfoLevel)) assert.Equal(t, ioutil.Discard, golog.Writer()) } + +func TestLoggingECSFields(t *testing.T) { + cfg := Config{ + Beat: "beat1", + Level: DebugLevel, + development: true, + ECSEnabled: true, + Files: FileConfig{ + Name: "beat1.log", + }, + } + ToObserverOutput()(&cfg) + Configure(cfg) + + logger := NewLogger("tester") + + logger.Debug("debug") + logs := ObserverLogs().TakeAll() + if assert.Len(t, logs, 1) { + if assert.Len(t, logs[0].Context, 2) { + assert.Equal(t, "service.name", logs[0].Context[0].Key) + assert.Equal(t, "beat1", logs[0].Context[0].String) + assert.Equal(t, "event.dataset", logs[0].Context[1].Key) + assert.Equal(t, "beat1.log", logs[0].Context[1].String) + } + } +}