diff --git a/logp/core.go b/logp/core.go index eb518a9b..0dbc1b0c 100644 --- a/logp/core.go +++ b/logp/core.go @@ -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. diff --git a/logp/core_test.go b/logp/core_test.go index cac0ce27..327669e1 100644 --- a/logp/core_test.go +++ b/logp/core_test.go @@ -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 {