Skip to content

Commit 2067ea4

Browse files
authored
Merge pull request #874 from sirupsen/fieldErrorBugFix
do not clear error formatting informative field
2 parents 9f04967 + 9abefb9 commit 2067ea4

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

entry.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
108108
for k, v := range entry.Data {
109109
data[k] = v
110110
}
111-
var fieldErr string
111+
fieldErr := entry.err
112112
for k, v := range fields {
113113
isErrField := false
114114
if t := reflect.TypeOf(v); t != nil {
@@ -120,9 +120,11 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
120120
}
121121
}
122122
if isErrField {
123-
fieldErr = fmt.Sprintf("can not add field %q", k)
124-
if entry.err != "" {
125-
fieldErr = entry.err + ", " + fieldErr
123+
tmp := fmt.Sprintf("can not add field %q", k)
124+
if fieldErr != "" {
125+
fieldErr = entry.err + ", " + tmp
126+
} else {
127+
fieldErr = tmp
126128
}
127129
} else {
128130
data[k] = v
@@ -133,7 +135,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
133135

134136
// Overrides the time of the Entry.
135137
func (entry *Entry) WithTime(t time.Time) *Entry {
136-
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t}
138+
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t, err: entry.err}
137139
}
138140

139141
// getPackageName reduces a fully qualified function name to the package name

entry_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"testing"
7+
"time"
78

89
"github.com/stretchr/testify/assert"
910
)
@@ -125,4 +126,16 @@ func TestEntryWithIncorrectField(t *testing.T) {
125126

126127
assert.Equal(eWithFunc.err, `can not add field "func"`)
127128
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
129+
130+
eWithFunc = eWithFunc.WithField("not_a_func", "it is a string")
131+
eWithFuncPtr = eWithFuncPtr.WithField("not_a_func", "it is a string")
132+
133+
assert.Equal(eWithFunc.err, `can not add field "func"`)
134+
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
135+
136+
eWithFunc = eWithFunc.WithTime(time.Now())
137+
eWithFuncPtr = eWithFuncPtr.WithTime(time.Now())
138+
139+
assert.Equal(eWithFunc.err, `can not add field "func"`)
140+
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
128141
}

0 commit comments

Comments
 (0)