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

Move addFields to ObjectEncoder to let logger#With be extendible #685

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion zapcore/console_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
context := c.jsonEncoder.Clone().(*jsonEncoder)
defer context.buf.Free()

addFields(context, extra)
context.AddFields(extra)
context.closeOpenNamespaces()
if context.buf.Len() == 0 {
return
Expand Down
2 changes: 1 addition & 1 deletion zapcore/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type ioCore struct {

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

Expand Down
2 changes: 2 additions & 0 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ type ObjectEncoder interface {
// be added. Applications can use namespaces to prevent key collisions when
// injecting loggers into sub-components or third-party libraries.
OpenNamespace(key string)
// AddFields adds more than multiple fields in once.
AddFields(fields []Field)
}

// ArrayEncoder is a strongly-typed, encoding-agnostic interface for adding
Expand Down
6 changes: 0 additions & 6 deletions zapcore/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,3 @@ func (f Field) Equals(other Field) bool {
return f == other
}
}

func addFields(enc ObjectEncoder, fields []Field) {
for i := range fields {
fields[i].AddTo(enc)
}
}
8 changes: 7 additions & 1 deletion zapcore/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ func (enc *jsonEncoder) OpenNamespace(key string) {
enc.openNamespaces++
}

func (enc *jsonEncoder) AddFields(fields []Field) {
for _, field := range fields {
field.AddTo(enc)
}
}

func (enc *jsonEncoder) AddString(key, val string) {
enc.addKey(key)
enc.AppendString(val)
Expand Down Expand Up @@ -358,7 +364,7 @@ func (enc *jsonEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
final.addElementSeparator()
final.buf.Write(enc.buf.Bytes())
}
addFields(final, fields)
final.AddFields(fields)
final.closeOpenNamespaces()
if ent.Stack != "" && final.StacktraceKey != "" {
final.AddString(final.StacktraceKey, ent.Stack)
Expand Down
7 changes: 7 additions & 0 deletions zapcore/memory_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func (m *MapObjectEncoder) OpenNamespace(k string) {
m.cur = ns
}

// AddFields implements ObjectEncoder.
func (m *MapObjectEncoder) AddFields(fields []Field) {
for _, field := range fields {
field.AddTo(m)
}
}

// sliceArrayEncoder is an ArrayEncoder backed by a simple []interface{}. Like
// the MapObjectEncoder, it's not designed for production use.
type sliceArrayEncoder struct {
Expand Down
9 changes: 9 additions & 0 deletions zapcore/memory_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,12 @@ func TestMapObjectEncoderReflectionFailures(t *testing.T) {
"Expected encoder to use empty values on errors.",
)
}

func TestMapObjectEncoderAddaFields(t *testing.T) {
enc := NewMapObjectEncoder()
enc.AddFields([]Field{
{Key: "AddStr", Type: StringType, String: "StrVal"},
{Key: "AddInt", Type: Int64Type, Integer: int64(1)},
})
assert.Equal(t, 2, len(enc.Fields))
}