From a572c9d1f674e774330ab0dda39e5902e184fab0 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Wed, 9 May 2018 03:51:52 -0700 Subject: [PATCH] Add missing support for zerolog marshable objects to Fields --- context.go | 2 +- event.go | 7 ++----- fields.go | 11 ++++++++++- log.go | 2 +- log_test.go | 3 ++- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/context.go b/context.go index 1f92dae..146a5df 100644 --- a/context.go +++ b/context.go @@ -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) diff --git a/event.go b/event.go index dffaf3e..7481a40 100644 --- a/event.go +++ b/event.go @@ -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] @@ -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. diff --git a/fields.go b/fields.go index 9d13f0c..8d8a62b 100644 --- a/fields.go +++ b/fields.go @@ -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: diff --git a/log.go b/log.go index 655954b..528ac77 100644 --- a/log.go +++ b/log.go @@ -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 { diff --git a/log_test.go b/log_test.go index 6503927..1175af2 100644 --- a/log_test.go +++ b/log_test.go @@ -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) } }