mirror of https://github.com/sirupsen/logrus.git
Merge pull request #874 from sirupsen/fieldErrorBugFix
do not clear error formatting informative field
This commit is contained in:
commit
2067ea4241
12
entry.go
12
entry.go
|
@ -108,7 +108,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||||
for k, v := range entry.Data {
|
for k, v := range entry.Data {
|
||||||
data[k] = v
|
data[k] = v
|
||||||
}
|
}
|
||||||
var fieldErr string
|
fieldErr := entry.err
|
||||||
for k, v := range fields {
|
for k, v := range fields {
|
||||||
isErrField := false
|
isErrField := false
|
||||||
if t := reflect.TypeOf(v); t != nil {
|
if t := reflect.TypeOf(v); t != nil {
|
||||||
|
@ -120,9 +120,11 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if isErrField {
|
if isErrField {
|
||||||
fieldErr = fmt.Sprintf("can not add field %q", k)
|
tmp := fmt.Sprintf("can not add field %q", k)
|
||||||
if entry.err != "" {
|
if fieldErr != "" {
|
||||||
fieldErr = entry.err + ", " + fieldErr
|
fieldErr = entry.err + ", " + tmp
|
||||||
|
} else {
|
||||||
|
fieldErr = tmp
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
data[k] = v
|
data[k] = v
|
||||||
|
@ -133,7 +135,7 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
|
||||||
|
|
||||||
// Overrides the time of the Entry.
|
// Overrides the time of the Entry.
|
||||||
func (entry *Entry) WithTime(t time.Time) *Entry {
|
func (entry *Entry) WithTime(t time.Time) *Entry {
|
||||||
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t}
|
return &Entry{Logger: entry.Logger, Data: entry.Data, Time: t, err: entry.err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPackageName reduces a fully qualified function name to the package name
|
// getPackageName reduces a fully qualified function name to the package name
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
@ -125,4 +126,16 @@ func TestEntryWithIncorrectField(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
||||||
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
||||||
|
|
||||||
|
eWithFunc = eWithFunc.WithField("not_a_func", "it is a string")
|
||||||
|
eWithFuncPtr = eWithFuncPtr.WithField("not_a_func", "it is a string")
|
||||||
|
|
||||||
|
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
||||||
|
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
||||||
|
|
||||||
|
eWithFunc = eWithFunc.WithTime(time.Now())
|
||||||
|
eWithFuncPtr = eWithFuncPtr.WithTime(time.Now())
|
||||||
|
|
||||||
|
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
||||||
|
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue