From 08429ad47d49ce26078270fbf6e573ef0801fd9b Mon Sep 17 00:00:00 2001 From: Juan Date: Sat, 29 May 2021 20:54:47 +0200 Subject: [PATCH 1/3] add more ECS fields to logs --- libbeat/logp/configure/logging.go | 4 +++- libbeat/logp/core.go | 13 ++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/libbeat/logp/configure/logging.go b/libbeat/logp/configure/logging.go index 43a32dd7f2f..f93033a8945 100644 --- a/libbeat/logp/configure/logging.go +++ b/libbeat/logp/configure/logging.go @@ -55,7 +55,9 @@ func Logging(beatName string, cfg *common.Config) error { return err } } - + if config.Files.Name == "" && config.ToFiles { + config.Files.Name = beatName + } applyFlags(&config) return logp.Configure(config) } diff --git a/libbeat/logp/core.go b/libbeat/logp/core.go index f4ccb2eece2..56469e05bec 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)} + if cfg.Files.Name != "" { + fields = append(fields, zap.String("event.dataset", cfg.Files.Name)) + } + 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.Files.Name)) rotator, err := file.NewFileRotator(filename, file.MaxSizeBytes(cfg.Files.MaxSize), From 9c84e26f8d323d3a4e5a7f744abd194621b70f3c Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 1 Jun 2021 09:49:09 +0200 Subject: [PATCH 2/3] allways set event.dataset --- libbeat/logp/config.go | 11 +++++++++++ libbeat/logp/configure/logging.go | 4 +--- libbeat/logp/core.go | 8 ++++---- 3 files changed, 16 insertions(+), 7 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/configure/logging.go b/libbeat/logp/configure/logging.go index f93033a8945..43a32dd7f2f 100644 --- a/libbeat/logp/configure/logging.go +++ b/libbeat/logp/configure/logging.go @@ -55,9 +55,7 @@ func Logging(beatName string, cfg *common.Config) error { return err } } - if config.Files.Name == "" && config.ToFiles { - config.Files.Name = beatName - } + applyFlags(&config) return logp.Configure(config) } diff --git a/libbeat/logp/core.go b/libbeat/logp/core.go index 56469e05bec..0bcf76f5edc 100644 --- a/libbeat/logp/core.go +++ b/libbeat/logp/core.go @@ -197,9 +197,9 @@ func makeOptions(cfg Config) []zap.Option { options = append(options, zap.Development()) } if cfg.ECSEnabled { - fields := []zap.Field{zap.String("service.name", cfg.Beat)} - if cfg.Files.Name != "" { - fields = append(fields, zap.String("event.dataset", cfg.Files.Name)) + fields := []zap.Field{ + zap.String("service.name", cfg.Beat), + zap.String("event.dataset", cfg.LogFilename()), } options = append(options, zap.Fields(fields...)) } @@ -233,7 +233,7 @@ func makeEventLogOutput(cfg Config) (zapcore.Core, error) { } func makeFileOutput(cfg Config) (zapcore.Core, error) { - filename := paths.Resolve(paths.Logs, filepath.Join(cfg.Files.Path, cfg.Files.Name)) + filename := paths.Resolve(paths.Logs, filepath.Join(cfg.Files.Path, cfg.LogFilename())) rotator, err := file.NewFileRotator(filename, file.MaxSizeBytes(cfg.Files.MaxSize), From b8cef4bd5ebd17308b4bee6f8642b63beeb73dc1 Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 1 Jun 2021 15:51:47 +0200 Subject: [PATCH 3/3] Add test --- libbeat/logp/core_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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) + } + } +}