diff --git a/internal/json/base.go b/internal/json/base.go index d6f8839e..62248e71 100644 --- a/internal/json/base.go +++ b/internal/json/base.go @@ -4,9 +4,8 @@ type Encoder struct{} // AppendKey appends a new key to the output JSON. func (e Encoder) AppendKey(dst []byte, key string) []byte { - if len(dst) > 1 && dst[len(dst)-1] != '{' { + if dst[len(dst)-1] != '{' { dst = append(dst, ',') } - dst = e.AppendString(dst, key) - return append(dst, ':') -} \ No newline at end of file + return append(e.AppendString(dst, key), ':') +} diff --git a/internal/json/types.go b/internal/json/types.go index bc8bc095..d1862426 100644 --- a/internal/json/types.go +++ b/internal/json/types.go @@ -379,11 +379,10 @@ func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { // to separate with existing content OR // 3. existing content has already other fields if o[0] == '{' { - if len(dst) == 0 { - o = o[1:] - } else { - o[0] = ',' + if len(dst) > 1 { + dst = append(dst, ',') } + o = o[1:] } else if len(dst) > 1 { dst = append(dst, ',') } diff --git a/log.go b/log.go index b1e7ac13..cbf68850 100644 --- a/log.go +++ b/log.go @@ -234,6 +234,10 @@ func (l Logger) With() Context { l.context = make([]byte, 0, 500) if context != nil { l.context = append(l.context, context...) + } else { + // This is needed for AppendKey to not check len of input + // thus making it inlinable + l.context = enc.AppendBeginMarker(l.context) } return Context{l} } @@ -415,7 +419,7 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event { if level != NoLevel { e.Str(LevelFieldName, LevelFieldMarshalFunc(level)) } - if l.context != nil && len(l.context) > 0 { + if l.context != nil && len(l.context) > 1 { e.buf = enc.AppendObjectData(e.buf, l.context) } return e