Skip to content

Commit

Permalink
Add missing support for zerolog marshable objects to Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed May 9, 2018
1 parent 79281e4 commit a572c9d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c Context) Array(key string, arr LogArrayMarshaler) Context {

// Object marshals an object that implement the LogObjectMarshaler interface.
func (c Context) Object(key string, obj LogObjectMarshaler) Context {
e := newEvent(levelWriterAdapter{ioutil.Discard}, 0, true)
e := newEvent(levelWriterAdapter{ioutil.Discard}, 0)
e.Object(key, obj)
c.l.context = appendObjectData(c.l.context, e.buf)
eventPool.Put(e)
Expand Down
7 changes: 2 additions & 5 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ type LogArrayMarshaler interface {
MarshalZerologArray(a *Array)
}

func newEvent(w LevelWriter, level Level, enabled bool) *Event {
if !enabled {
return &Event{}
}
func newEvent(w LevelWriter, level Level) *Event {
e := eventPool.Get().(*Event)
e.buf = e.buf[:0]
e.h = e.h[:0]
Expand Down Expand Up @@ -144,7 +141,7 @@ func (e *Event) Dict(key string, dict *Event) *Event {
// Call usual field methods like Str, Int etc to add fields to this
// event and give it as argument the *Event.Dict method.
func Dict() *Event {
return newEvent(nil, 0, true)
return newEvent(nil, 0)
}

// Array adds the field key with an array to the event context.
Expand Down
11 changes: 10 additions & 1 deletion fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ func appendFields(dst []byte, fields map[string]interface{}) []byte {
sort.Strings(keys)
for _, key := range keys {
dst = appendKey(dst, key)
switch val := fields[key].(type) {
val := fields[key]
if val, ok := val.(LogObjectMarshaler); ok {
e := newEvent(nil, 0)
e.buf = e.buf[:0]
e.appendObject(val)
dst = append(dst, e.buf...)
eventPool.Put(e)
continue
}
switch val := val.(type) {
case string:
dst = appendString(dst, val)
case []byte:
Expand Down
2 changes: 1 addition & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event {
if !enabled {
return nil
}
e := newEvent(l.w, level, true)
e := newEvent(l.w, level)
e.done = done
e.ch = l.hooks
if level != NoLevel {
Expand Down
3 changes: 2 additions & 1 deletion log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ func TestFieldsMap(t *testing.T) {
"ipv6": net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34},
"dur": 1 * time.Second,
"time": time.Time{},
"obj": obj{"a", "b", 1},
}).Msg("")
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":true,"bytes":"bar","dur":1000,"error":"some error","float32":11,"float64":12,"int":1,"int16":3,"int32":4,"int64":5,"int8":2,"ipv6":"2001:db8:85a3::8a2e:370:7334","nil":null,"string":"foo","time":"0001-01-01T00:00:00Z","uint":6,"uint16":8,"uint32":9,"uint64":10,"uint8":7}`+"\n"; got != want {
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":true,"bytes":"bar","dur":1000,"error":"some error","float32":11,"float64":12,"int":1,"int16":3,"int32":4,"int64":5,"int8":2,"ipv6":"2001:db8:85a3::8a2e:370:7334","nil":null,"obj":{"Pub":"a","Tag":"b","priv":1},"string":"foo","time":"0001-01-01T00:00:00Z","uint":6,"uint16":8,"uint32":9,"uint64":10,"uint8":7}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
Expand Down

0 comments on commit a572c9d

Please sign in to comment.