Add missing support for zerolog marshable objects to Fields
This commit is contained in:
parent
79281e4bf6
commit
a572c9d1f6
|
@ -52,7 +52,7 @@ func (c Context) Array(key string, arr LogArrayMarshaler) Context {
|
||||||
|
|
||||||
// Object marshals an object that implement the LogObjectMarshaler interface.
|
// Object marshals an object that implement the LogObjectMarshaler interface.
|
||||||
func (c Context) Object(key string, obj LogObjectMarshaler) Context {
|
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)
|
e.Object(key, obj)
|
||||||
c.l.context = appendObjectData(c.l.context, e.buf)
|
c.l.context = appendObjectData(c.l.context, e.buf)
|
||||||
eventPool.Put(e)
|
eventPool.Put(e)
|
||||||
|
|
7
event.go
7
event.go
|
@ -41,10 +41,7 @@ type LogArrayMarshaler interface {
|
||||||
MarshalZerologArray(a *Array)
|
MarshalZerologArray(a *Array)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newEvent(w LevelWriter, level Level, enabled bool) *Event {
|
func newEvent(w LevelWriter, level Level) *Event {
|
||||||
if !enabled {
|
|
||||||
return &Event{}
|
|
||||||
}
|
|
||||||
e := eventPool.Get().(*Event)
|
e := eventPool.Get().(*Event)
|
||||||
e.buf = e.buf[:0]
|
e.buf = e.buf[:0]
|
||||||
e.h = e.h[: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
|
// Call usual field methods like Str, Int etc to add fields to this
|
||||||
// event and give it as argument the *Event.Dict method.
|
// event and give it as argument the *Event.Dict method.
|
||||||
func Dict() *Event {
|
func Dict() *Event {
|
||||||
return newEvent(nil, 0, true)
|
return newEvent(nil, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array adds the field key with an array to the event context.
|
// Array adds the field key with an array to the event context.
|
||||||
|
|
11
fields.go
11
fields.go
|
@ -14,7 +14,16 @@ func appendFields(dst []byte, fields map[string]interface{}) []byte {
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
dst = appendKey(dst, key)
|
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:
|
case string:
|
||||||
dst = appendString(dst, val)
|
dst = appendString(dst, val)
|
||||||
case []byte:
|
case []byte:
|
||||||
|
|
2
log.go
2
log.go
|
@ -374,7 +374,7 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event {
|
||||||
if !enabled {
|
if !enabled {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
e := newEvent(l.w, level, true)
|
e := newEvent(l.w, level)
|
||||||
e.done = done
|
e.done = done
|
||||||
e.ch = l.hooks
|
e.ch = l.hooks
|
||||||
if level != NoLevel {
|
if level != NoLevel {
|
||||||
|
|
|
@ -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},
|
"ipv6": net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34},
|
||||||
"dur": 1 * time.Second,
|
"dur": 1 * time.Second,
|
||||||
"time": time.Time{},
|
"time": time.Time{},
|
||||||
|
"obj": obj{"a", "b", 1},
|
||||||
}).Msg("")
|
}).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)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue