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

Allow retrieval of previously added fields from the Core #1395

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions zapcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Core interface {
Write(Entry, []Field) error
// Sync flushes buffered logs (if any).
Sync() error
// Fields returns any added structured context from the Core.
Fields() []Field
}

type nopCore struct{}
Expand All @@ -50,6 +52,7 @@ type nopCore struct{}
func NewNopCore() Core { return nopCore{} }
func (nopCore) Enabled(Level) bool { return false }
func (n nopCore) With([]Field) Core { return n }
func (n nopCore) Fields() []Field { return nil }
func (nopCore) Check(_ Entry, ce *CheckedEntry) *CheckedEntry { return ce }
func (nopCore) Write(Entry, []Field) error { return nil }
func (nopCore) Sync() error { return nil }
Expand All @@ -65,8 +68,9 @@ func NewCore(enc Encoder, ws WriteSyncer, enab LevelEnabler) Core {

type ioCore struct {
LevelEnabler
enc Encoder
out WriteSyncer
enc Encoder
out WriteSyncer
fields []Field
}

var (
Expand All @@ -80,10 +84,15 @@ func (c *ioCore) Level() Level {

func (c *ioCore) With(fields []Field) Core {
clone := c.clone()
clone.fields = append(clone.fields, fields...)
addFields(clone.enc, fields)
return clone
}

func (c *ioCore) Fields() []Field {
return c.fields
}

func (c *ioCore) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
Expand Down Expand Up @@ -118,5 +127,6 @@ func (c *ioCore) clone() *ioCore {
LevelEnabler: c.LevelEnabler,
enc: c.enc.Clone(),
out: c.out,
fields: c.fields,
}
}
18 changes: 18 additions & 0 deletions zapcore/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,21 @@ func TestIOCoreWriteFailure(t *testing.T) {
// Should log the error.
assert.Error(t, err, "Expected writing Entry to fail.")
}

func TestIOCoreFields(t *testing.T) {
fields := []Field{makeInt64Field("k", 1)}

core := NewCore(
NewJSONEncoder(testEncoderConfig()),
Lock(os.Stderr),
DebugLevel,
).With(fields)

expectedFields := core.Fields()
assert.Len(t, expectedFields, 1, "Expected one field.")
assert.Equal(t, fields, expectedFields, "Unexpected Fields.")

core = core.With([]Field{makeInt64Field("w", 2)})
expectedFields = core.Fields()
assert.Len(t, expectedFields, 2, "Expected two fields.")
}
4 changes: 4 additions & 0 deletions zapcore/increase_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (c *levelFilterCore) With(fields []Field) Core {
return &levelFilterCore{c.core.With(fields), c.level}
}

func (c *levelFilterCore) Fields() []Field {
return c.core.Fields()
}

func (c *levelFilterCore) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
if !c.Enabled(ent.Level) {
return ce
Expand Down
1 change: 1 addition & 0 deletions zapcore/sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (c *countingCore) Write(Entry, []Field) error {
}

func (c *countingCore) With([]Field) Core { return c }
func (c *countingCore) Fields() []Field { return nil }
func (*countingCore) Enabled(Level) bool { return true }
func (*countingCore) Sync() error { return nil }

Expand Down
10 changes: 10 additions & 0 deletions zapcore/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (mc multiCore) With(fields []Field) Core {
return clone
}

func (mc multiCore) Fields() []Field {
var fields []Field

if len(mc) == 0 {
return fields
}

return mc[0].Fields()
}

func (mc multiCore) Level() Level {
minLvl := _maxLevel // mc is never empty
for i := range mc {
Expand Down
11 changes: 11 additions & 0 deletions zapcore/tee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ func TestTeeWith(t *testing.T) {
})
}

func TestTeeFields(t *testing.T) {
withTee(func(tee Core, debugLogs, warnLogs *observer.ObservedLogs) {
fields := []Field{makeInt64Field("k", 42)}
tee = tee.With(fields)

expectedFields := tee.Fields()
assert.Greater(t, len(expectedFields), 0, "Expected non-empty fields.")
assert.Equal(t, fields, expectedFields, "Unexpected fields.")
})
}

func TestTeeEnabled(t *testing.T) {
infoLogger, _ := observer.New(InfoLevel)
warnLogger, _ := observer.New(WarnLevel)
Expand Down
4 changes: 4 additions & 0 deletions zaptest/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (co *contextObserver) With(fields []zapcore.Field) zapcore.Core {
}
}

func (co *contextObserver) Fields() []zapcore.Field {
return co.context
}

func (co *contextObserver) Write(ent zapcore.Entry, fields []zapcore.Field) error {
all := make([]zapcore.Field, 0, len(fields)+len(co.context))
all = append(all, co.context...)
Expand Down