Skip to content

Commit

Permalink
zerolog: fix WithFields() implementation as map[string]any isn't dire…
Browse files Browse the repository at this point in the history
…ctly supported by zerolog

the event is a buffer, so calling Interface for each item is the same
as calling zerolog's Fields() would do

Signed-off-by: Alejandro Mery <amery@jpi.io>
  • Loading branch information
amery committed Jan 18, 2023
1 parent 51fccc0 commit 695c8f4
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion zerolog/zerolog.go
Expand Up @@ -3,6 +3,7 @@ package zerolog

import (
"fmt"
"sort"
"strings"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -128,7 +129,20 @@ func (zl *Logger) WithField(label string, value any) slog.Logger {
// WithFields adds fields to the Event Context
func (zl *Logger) WithFields(fields map[string]any) slog.Logger {
if zl.Enabled() {
zl.event.Fields(fields)
if l := len(fields); l > 0 {
// sort keys
keys := make([]string, 0, l)
for key := range fields {
keys = append(keys, key)
}
sort.Strings(keys)

// append in order
ev := zl.event
for _, key := range keys {
ev.Interface(key, fields[key])
}
}
}
return zl
}
Expand Down

0 comments on commit 695c8f4

Please sign in to comment.