Skip to content

Commit

Permalink
Merge pull request #121 from thockin/master
Browse files Browse the repository at this point in the history
funcr: Fix bug in repeated calls to WithValues
  • Loading branch information
pohly committed Nov 25, 2021
2 parents 8d00e35 + 2750c5e commit eeeb624
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions funcr/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ func ExampleOptions_renderHooks() {
RenderValuesHook: valuesAsObject,
})
log = log.WithName("MyLogger")
log = log.WithValues("savedKey", "savedValue")
log = log.WithValues("savedKey1", "savedVal1")
log = log.WithValues("savedKey2", "savedVal2")
log.Info("the message", "key", "value")
// Output: {"log:logger":"MyLogger","log:level":0,"log:msg":"the message","labels":{"savedKey":"savedValue"},"key":"value"}
// Output: {"log:logger":"MyLogger","log:level":0,"log:msg":"the message","labels":{"savedKey1":"savedVal1","savedKey2":"savedVal2"},"key":"value"}
}

func ExamplePseudoStruct() {
Expand Down
7 changes: 4 additions & 3 deletions funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,16 @@ func (f *Formatter) AddName(name string) {
func (f *Formatter) AddValues(kvList []interface{}) {
// Three slice args forces a copy.
n := len(f.values)
vals := f.values[:n:n]
vals = append(vals, kvList...)
f.values = append(f.values[:n:n], kvList...)

vals := f.values
if hook := f.opts.RenderValuesHook; hook != nil {
vals = hook(f.sanitize(vals))
}

// Pre-render values, so we don't have to do it on each Info/Error call.
buf := bytes.NewBuffer(make([]byte, 0, 1024))
f.values = f.flatten(buf, vals, false, true) // escape user-provided keys
f.flatten(buf, vals, false, true) // escape user-provided keys
f.valuesStr = buf.String()
}

Expand Down

0 comments on commit eeeb624

Please sign in to comment.