-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Further error-logging improvements #333
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,16 @@ | |
package zap | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var _errArrayElemPool = sync.Pool{New: func() interface{} { | ||
return &errArrayElem{} | ||
}} | ||
|
||
// Array constructs a field with the given key and ArrayMarshaler. It provides | ||
// a flexible, but still type-safe and efficient, way to add array-like types | ||
// to the logging context. The struct's MarshalLogArray method is called lazily. | ||
|
@@ -311,7 +316,26 @@ func (errs errArray) MarshalLogArray(arr zapcore.ArrayEncoder) error { | |
if errs[i] == nil { | ||
continue | ||
} | ||
arr.AppendString(errs[i].Error()) | ||
// To represent each error as an object with an "error" attribute and | ||
// potentially an "errorVerbose" attribute, we need to wrap it in a | ||
// type that implements LogObjectMarshaler. To prevent this from | ||
// allocating, pool the wrapper type. | ||
elem := _errArrayElemPool.Get().(*errArrayElem) | ||
elem.error = errs[i] | ||
arr.AppendObject(elem) | ||
elem.error = nil | ||
_errArrayElemPool.Put(elem) | ||
} | ||
return nil | ||
} | ||
|
||
type errArrayElem struct { | ||
error | ||
} | ||
|
||
func (e *errArrayElem) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
// Re-use the error field's logic, which supports non-standard error types. | ||
Error(e.error).AddTo(enc) | ||
return nil | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove extra newline? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,11 @@ func TestArrayWrappers(t *testing.T) { | |
{"uint16s", Uint16s("", []uint16{1, 2}), []interface{}{uint16(1), uint16(2)}}, | ||
{"uint8s", Uint8s("", []uint8{1, 2}), []interface{}{uint8(1), uint8(2)}}, | ||
{"uintptrs", Uintptrs("", []uintptr{1, 2}), []interface{}{uintptr(1), uintptr(2)}}, | ||
{"errors", Errors("", []error{nil, errors.New("foo"), nil, errors.New("bar")}), []interface{}{"foo", "bar"}}, | ||
{ | ||
"errors", | ||
Errors("", []error{nil, errors.New("foo"), nil, errors.New("bar")}), | ||
[]interface{}{map[string]interface{}{"error": "foo"}, map[string]interface{}{"error": "bar"}}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add a test which also adds There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can. Those code paths are already covered, but doesn't hurt to do it again. |
||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does mean our output for
[]error
ends up being a list of objects which have a keyerror
. I don't have strong opinions on this, but it does seem a little strange.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's definitely a little odd. I'm honestly not sure what else to do - it seems worse to have arrays that mix strings and objects. Would you prefer that? Or separate arrays of the errors and verbose errors?