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

Add more ECS fields to logs #25998

Merged
merged 4 commits into from
Jun 11, 2021
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
11 changes: 11 additions & 0 deletions libbeat/logp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 8 additions & 5 deletions libbeat/logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
axw marked this conversation as resolved.
Show resolved Hide resolved
filename := paths.Resolve(paths.Logs, filepath.Join(cfg.Files.Path, cfg.LogFilename()))

rotator, err := file.NewFileRotator(filename,
file.MaxSizeBytes(cfg.Files.MaxSize),
Expand Down
27 changes: 27 additions & 0 deletions libbeat/logp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}