File tree 2 files changed +20
-5
lines changed
2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -108,7 +108,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
108
108
for k , v := range entry .Data {
109
109
data [k ] = v
110
110
}
111
- var fieldErr string
111
+ fieldErr := entry . err
112
112
for k , v := range fields {
113
113
isErrField := false
114
114
if t := reflect .TypeOf (v ); t != nil {
@@ -120,9 +120,11 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
120
120
}
121
121
}
122
122
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
126
128
}
127
129
} else {
128
130
data [k ] = v
@@ -133,7 +135,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
133
135
134
136
// Overrides the time of the Entry.
135
137
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 }
137
139
}
138
140
139
141
// getPackageName reduces a fully qualified function name to the package name
Original file line number Diff line number Diff line change 4
4
"bytes"
5
5
"fmt"
6
6
"testing"
7
+ "time"
7
8
8
9
"github.com/stretchr/testify/assert"
9
10
)
@@ -125,4 +126,16 @@ func TestEntryWithIncorrectField(t *testing.T) {
125
126
126
127
assert .Equal (eWithFunc .err , `can not add field "func"` )
127
128
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"` )
128
141
}
You can’t perform that action at this time.
0 commit comments