diff --git a/sdlogr.go b/sdlogr.go index 0d5bdb3..5d8addb 100644 --- a/sdlogr.go +++ b/sdlogr.go @@ -71,7 +71,7 @@ func NewWithOptions(opts Options) logr.Logger { depth: opts.Depth + 1, logCallerInfo: opts.LogCallerInfo, out: opts.Out, - valuesMap: make(map[string]interface{}, 1), + valuesMap: make(map[string]string, 1), } return logr.New(&l) } @@ -80,7 +80,7 @@ type sdLogr struct { level int depth int prefix string - valuesMap map[string]interface{} + valuesMap map[string]string valuesStr string out io.Writer logCallerInfo bool @@ -197,21 +197,24 @@ func (l sdLogr) WithValues(kvList ...interface{}) logr.LogSink { n++ } + l.valuesStr = "" + var lastKey *string for i := 0; i < n; i++ { - k := kvList[i] - if k == "" { - k = emptyStringPlaceholder + kvi := kvList[i] + if kvi == "" { + kvi = emptyStringPlaceholder + } + kv := fmt.Sprintf("%v", deref(kvi)) + if (i & 1) == 0 { + // kv is key + lastKey = &kv + } else { + // kv is value + l.valuesMap[*lastKey] = kv } - i++ - l.valuesMap[fmt.Sprintf("%v", deref(k))] = kvList[i] } - // rebuild the string - l.valuesStr = "" for k, v := range l.valuesMap { - if v == "" { - v = emptyStringPlaceholder - } l.valuesStr += k + ": " + fmt.Sprintf("%v", deref(v)) + ", " } return &l diff --git a/sdlogr_test.go b/sdlogr_test.go index 4a208bf..e885604 100644 --- a/sdlogr_test.go +++ b/sdlogr_test.go @@ -219,7 +219,7 @@ func TestWithValues(t *testing.T) { log = log.WithValues("vv", "vvk") log.Info("msg1") log = log.WithValues("vv", "vvk") - log.Info("msg2") + log.Info("msg2", "vv", "v") log = log.WithValues("vv1", "vvk") log.Info("msg3") }