Fixed prefixFieldClashes for TextFormatter and added coverage

This commit is contained in:
Neil Isaac 2018-06-18 21:32:35 -04:00
parent 21326f6618
commit 5d60369ef3
3 changed files with 20 additions and 5 deletions

View File

@ -34,15 +34,18 @@ func prefixFieldClashes(data Fields, fieldMap FieldMap) {
timeKey := fieldMap.resolve(FieldKeyTime) timeKey := fieldMap.resolve(FieldKeyTime)
if t, ok := data[timeKey]; ok { if t, ok := data[timeKey]; ok {
data["fields."+timeKey] = t data["fields."+timeKey] = t
delete(data, timeKey)
} }
msgKey := fieldMap.resolve(FieldKeyMsg) msgKey := fieldMap.resolve(FieldKeyMsg)
if m, ok := data[msgKey]; ok { if m, ok := data[msgKey]; ok {
data["fields."+msgKey] = m data["fields."+msgKey] = m
delete(data, msgKey)
} }
levelKey := fieldMap.resolve(FieldKeyLevel) levelKey := fieldMap.resolve(FieldKeyLevel)
if l, ok := data[levelKey]; ok { if l, ok := data[levelKey]; ok {
data["fields."+levelKey] = l data["fields."+levelKey] = l
delete(data, levelKey)
} }
} }

View File

@ -51,7 +51,6 @@ type TextFormatter struct {
// be desired. // be desired.
DisableSorting bool DisableSorting bool
// Disables the truncation of the level text to 4 characters. // Disables the truncation of the level text to 4 characters.
DisableLevelTruncation bool DisableLevelTruncation bool
@ -81,7 +80,8 @@ func (f *TextFormatter) init(entry *Entry) {
// Format renders a single log entry // Format renders a single log entry
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
var b *bytes.Buffer prefixFieldClashes(entry.Data, f.FieldMap)
keys := make([]string, 0, len(entry.Data)) keys := make([]string, 0, len(entry.Data))
for k := range entry.Data { for k := range entry.Data {
keys = append(keys, k) keys = append(keys, k)
@ -90,14 +90,14 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
if !f.DisableSorting { if !f.DisableSorting {
sort.Strings(keys) sort.Strings(keys)
} }
var b *bytes.Buffer
if entry.Buffer != nil { if entry.Buffer != nil {
b = entry.Buffer b = entry.Buffer
} else { } else {
b = &bytes.Buffer{} b = &bytes.Buffer{}
} }
prefixFieldClashes(entry.Data, emptyFieldMap)
f.Do(func() { f.init(entry) }) f.Do(func() { f.init(entry) })
isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors

View File

@ -191,6 +191,12 @@ func TestTextFormatterFieldMap(t *testing.T) {
Message: "oh hi", Message: "oh hi",
Level: WarnLevel, Level: WarnLevel,
Time: time.Date(1981, time.February, 24, 4, 28, 3, 100, time.UTC), Time: time.Date(1981, time.February, 24, 4, 28, 3, 100, time.UTC),
Data: Fields{
"field1": "f1",
"message": "messagefield",
"somelevel": "levelfield",
"timeywimey": "timeywimeyfield",
},
} }
b, err := formatter.Format(entry) b, err := formatter.Format(entry)
@ -199,7 +205,13 @@ func TestTextFormatterFieldMap(t *testing.T) {
} }
assert.Equal(t, assert.Equal(t,
`timeywimey="1981-02-24T04:28:03Z" somelevel=warning message="oh hi"`+"\n", `timeywimey="1981-02-24T04:28:03Z" `+
`somelevel=warning `+
`message="oh hi" `+
`field1=f1 `+
`fields.message=messagefield `+
`fields.somelevel=levelfield `+
`fields.timeywimey=timeywimeyfield`+"\n",
string(b), string(b),
"Formatted doesn't respect correct FieldMap") "Formatted doesn't respect correct FieldMap")
} }