Add missing support for zerolog marshable objects to Fields

This commit is contained in:
Olivier Poitrey 2018-05-09 03:51:52 -07:00
parent 79281e4bf6
commit a572c9d1f6
5 changed files with 16 additions and 9 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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:

2
log.go
View File

@ -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 {

View File

@ -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)
}
}