Skip to content

Commit 1b263ef

Browse files
authored
Add support for with_fields configuration (#290)
* add fields
1 parent ed5e3c2 commit 1b263ef

File tree

2 files changed

+65
-24
lines changed

2 files changed

+65
-24
lines changed

logp/core.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ func ConfigureWithCore(loggerCfg Config, core zapcore.Core) error {
164164
)
165165

166166
level = zap.NewAtomicLevelAt(loggerCfg.Level.ZapLevel())
167+
168+
if loggerCfg.WithFields != nil {
169+
fields := make([]zapcore.Field, 0, len(loggerCfg.WithFields))
170+
for key, value := range loggerCfg.WithFields {
171+
fields = append(fields, zap.Any(key, value))
172+
}
173+
core = core.With(fields)
174+
}
175+
167176
sink = wrappedCore(core)
168177

169178
// Enabled selectors when debug is enabled.

logp/core_test.go

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -707,33 +707,65 @@ func TestConfigureWithCore(t *testing.T) {
707707
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
708708
zapcore.AddSync(&b),
709709
zapcore.InfoLevel)
710-
err := ConfigureWithCore(Config{}, core)
711-
if err != nil {
712-
t.Fatalf("Unexpected err: %s", err)
713-
}
714-
Info("The quick brown %s jumped over the lazy %s.", "fox", "dog")
715-
var r map[string]interface{}
716710

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

722-
val, prs := r["msg"]
723-
if !prs {
724-
t.Fatalf("expected 'msg' field not present in '%s'", b.String())
725-
}
726-
if val != testMsg {
727-
t.Fatalf("expected msg of '%s', got '%s'", testMsg, val)
728-
}
719+
err = json.Unmarshal(b.Bytes(), &r)
720+
if err != nil {
721+
t.Fatalf("unable to json unmarshal '%s': %s", b.String(), err)
722+
}
723+
724+
val, prs := r["msg"]
725+
if !prs {
726+
t.Fatalf("expected 'msg' field not present in '%s'", b.String())
727+
}
728+
if val != testMsg {
729+
t.Fatalf("expected msg of '%s', got '%s'", testMsg, val)
730+
}
731+
val, prs = r["level"]
732+
if !prs {
733+
t.Fatalf("expected 'level' field not present in '%s'", b.String())
734+
}
735+
if val != "info" {
736+
t.Fatalf("expected log.level of 'info', got '%s'", val)
737+
}
738+
})
739+
740+
b.Reset()
741+
t.Run("test `with_fields` config", func(t *testing.T) {
742+
config := Config{
743+
WithFields: map[string]any{
744+
"component": "elastic-agent",
745+
},
746+
}
747+
err := ConfigureWithCore(config, core)
748+
if err != nil {
749+
t.Fatalf("Unexpected err: %s", err)
750+
}
751+
Info("The quick brown %s jumped over the lazy %s.", "fox", "dog")
752+
var r map[string]interface{}
753+
754+
err = json.Unmarshal(b.Bytes(), &r)
755+
if err != nil {
756+
t.Fatalf("unable to json unmarshal '%s': %s", b.String(), err)
757+
}
758+
// test `with_fields` works correctly
759+
expectedMsg := "elastic-agent"
760+
val, prs := r["component"]
761+
if !prs {
762+
t.Fatalf("expected 'component' field not present in '%s'", b.String())
763+
}
764+
if val != expectedMsg {
765+
t.Fatalf("expected msg of '%s', got '%s'", expectedMsg, val)
766+
}
767+
})
729768

730-
val, prs = r["level"]
731-
if !prs {
732-
t.Fatalf("expected 'level' field not present in '%s'", b.String())
733-
}
734-
if val != "info" {
735-
t.Fatalf("expected log.level of 'info', got '%s'", val)
736-
}
737769
}
738770

739771
func strField(key, val string) zapcore.Field {

0 commit comments

Comments
 (0)