diff --git a/zapcore/field.go b/zapcore/field.go index 8c279a46e..d8a245ecb 100644 --- a/zapcore/field.go +++ b/zapcore/field.go @@ -152,12 +152,16 @@ func (f Field) AddTo(enc ObjectEncoder) { enc.AddString(f.Key, f.Interface.(fmt.Stringer).String()) case ErrorType: val := f.Interface.(error) + basic := val.Error() + enc.AddString(f.Key, basic) if fancy, ok := val.(fmt.Formatter); ok { - // This is a rich error type, like those produced by - // github.com/pkg/errors. - enc.AddString(f.Key+"Verbose", fmt.Sprintf("%+v", fancy)) + verbose := fmt.Sprintf("%+v", fancy) + if verbose != basic { + // This is a rich error type, like those produced by + // github.com/pkg/errors. + enc.AddString(f.Key+"Verbose", verbose) + } } - enc.AddString(f.Key, val.Error()) case SkipType: break default: diff --git a/zapcore/field_test.go b/zapcore/field_test.go index a84372751..368e7f3d0 100644 --- a/zapcore/field_test.go +++ b/zapcore/field_test.go @@ -23,6 +23,7 @@ package zapcore_test import ( "errors" "fmt" + "io" "math" "testing" "time" @@ -36,11 +37,19 @@ import ( type users int func (u users) String() string { - return fmt.Sprintf("%d users", u) + return fmt.Sprintf("%d users", int(u)) } func (u users) Error() string { - return fmt.Sprintf("%d too many users", u) + return fmt.Sprintf("%d too many users", int(u)) +} + +func (u users) Format(s fmt.State, verb rune) { + // Implement fmt.Formatter, but don't add any information beyond the basic + // Error method. + if verb == 'v' && s.Flag('+') { + io.WriteString(s, u.Error()) + } } func (u users) MarshalLogObject(enc ObjectEncoder) error {