Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AppendKey in json inlinable #226

Merged
merged 2 commits into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions internal/json/base.go
Expand Up @@ -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, ':')
}
return append(e.AppendString(dst, key), ':')
}
7 changes: 3 additions & 4 deletions internal/json/types.go
Expand Up @@ -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, ',')
}
Expand Down
6 changes: 5 additions & 1 deletion log.go
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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
Expand Down