Fix key of field without quoting issue

This commit is contained in:
Jamie 2020-08-14 22:17:40 +08:00
parent 64a59449f3
commit 0fa5edc3dc
2 changed files with 16 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"time"
@ -138,7 +139,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
fieldErr = tmp
}
} else {
data[k] = v
data[strings.TrimPrefix(strings.TrimSuffix(strconv.Quote(k), `"`), `"`)] = v
}
}
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr, Context: entry.Context}

View File

@ -254,3 +254,17 @@ func TestEntryReportCallerRace(t *testing.T) {
entry.Info("should not race")
}()
}
func TestEntryWithHardcodedField(t *testing.T) {
out := new(bytes.Buffer)
l := New()
l.SetOutput(out)
l.WithFields(Fields{
"animal": "walrus",
"stu\xff": "test\"\xff\xff\xff\xff\x30\x00\x00\x00\x00test\xee\x81\x9a",
}).Info("A bug\nwas verified")
assert.Contains(t, out.String(), "animal")
assert.Contains(t, out.String(), `stu\xff`)
}