Skip to content
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
9 changes: 9 additions & 0 deletions logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ func ConfigureWithCore(loggerCfg Config, core zapcore.Core) error {
)

level = zap.NewAtomicLevelAt(loggerCfg.Level.ZapLevel())

if loggerCfg.WithFields != nil {
fields := make([]zapcore.Field, 0, len(loggerCfg.WithFields))
for key, value := range loggerCfg.WithFields {
fields = append(fields, zap.Any(key, value))
}
core = core.With(fields)
}

sink = wrappedCore(core)

// Enabled selectors when debug is enabled.
Expand Down
80 changes: 56 additions & 24 deletions logp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,33 +707,65 @@ func TestConfigureWithCore(t *testing.T) {
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(&b),
zapcore.InfoLevel)
err := ConfigureWithCore(Config{}, core)
if err != nil {
t.Fatalf("Unexpected err: %s", err)
}
Info("The quick brown %s jumped over the lazy %s.", "fox", "dog")
var r map[string]interface{}

err = json.Unmarshal(b.Bytes(), &r)
if err != nil {
t.Fatalf("unable to json unmarshal '%s': %s", b.String(), err)
}
t.Run("test default config", func(t *testing.T) {
err := ConfigureWithCore(Config{}, core)
if err != nil {
t.Fatalf("Unexpected err: %s", err)
}
Info("The quick brown %s jumped over the lazy %s.", "fox", "dog")
var r map[string]interface{}

val, prs := r["msg"]
if !prs {
t.Fatalf("expected 'msg' field not present in '%s'", b.String())
}
if val != testMsg {
t.Fatalf("expected msg of '%s', got '%s'", testMsg, val)
}
err = json.Unmarshal(b.Bytes(), &r)
if err != nil {
t.Fatalf("unable to json unmarshal '%s': %s", b.String(), err)
}

val, prs := r["msg"]
if !prs {
t.Fatalf("expected 'msg' field not present in '%s'", b.String())
}
if val != testMsg {
t.Fatalf("expected msg of '%s', got '%s'", testMsg, val)
}
val, prs = r["level"]
if !prs {
t.Fatalf("expected 'level' field not present in '%s'", b.String())
}
if val != "info" {
t.Fatalf("expected log.level of 'info', got '%s'", val)
}
})

b.Reset()
t.Run("test `with_fields` config", func(t *testing.T) {
config := Config{
WithFields: map[string]any{
"component": "elastic-agent",
},
}
err := ConfigureWithCore(config, core)
if err != nil {
t.Fatalf("Unexpected err: %s", err)
}
Info("The quick brown %s jumped over the lazy %s.", "fox", "dog")
var r map[string]interface{}

err = json.Unmarshal(b.Bytes(), &r)
if err != nil {
t.Fatalf("unable to json unmarshal '%s': %s", b.String(), err)
}
// test `with_fields` works correctly
expectedMsg := "elastic-agent"
val, prs := r["component"]
if !prs {
t.Fatalf("expected 'component' field not present in '%s'", b.String())
}
if val != expectedMsg {
t.Fatalf("expected msg of '%s', got '%s'", expectedMsg, val)
}
})

val, prs = r["level"]
if !prs {
t.Fatalf("expected 'level' field not present in '%s'", b.String())
}
if val != "info" {
t.Fatalf("expected log.level of 'info', got '%s'", val)
}
}

func strField(key, val string) zapcore.Field {
Expand Down
Loading