mirror of https://github.com/sirupsen/logrus.git
Merge pull request #870 from smacker/skip_func_pointer_type_value_in_fields
Skip func pointer type value in fields
This commit is contained in:
commit
93ebe33ece
19
entry.go
19
entry.go
|
@ -108,18 +108,27 @@ 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 field_err string
|
var fieldErr string
|
||||||
for k, v := range fields {
|
for k, v := range fields {
|
||||||
if t := reflect.TypeOf(v); t != nil && t.Kind() == reflect.Func {
|
isErrField := false
|
||||||
field_err = fmt.Sprintf("can not add field %q", k)
|
if t := reflect.TypeOf(v); t != nil {
|
||||||
|
switch t.Kind() {
|
||||||
|
case reflect.Func:
|
||||||
|
isErrField = true
|
||||||
|
case reflect.Ptr:
|
||||||
|
isErrField = t.Elem().Kind() == reflect.Func
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isErrField {
|
||||||
|
fieldErr = fmt.Sprintf("can not add field %q", k)
|
||||||
if entry.err != "" {
|
if entry.err != "" {
|
||||||
field_err = entry.err + ", " + field_err
|
fieldErr = entry.err + ", " + fieldErr
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
data[k] = v
|
data[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: field_err}
|
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overrides the time of the Entry.
|
// Overrides the time of the Entry.
|
||||||
|
|
|
@ -113,3 +113,16 @@ func TestEntryHooksPanic(t *testing.T) {
|
||||||
entry := NewEntry(logger)
|
entry := NewEntry(logger)
|
||||||
entry.Info(badMessage)
|
entry.Info(badMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEntryWithIncorrectField(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
fn := func() {}
|
||||||
|
|
||||||
|
e := Entry{}
|
||||||
|
eWithFunc := e.WithFields(Fields{"func": fn})
|
||||||
|
eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn})
|
||||||
|
|
||||||
|
assert.Equal(eWithFunc.err, `can not add field "func"`)
|
||||||
|
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue