Skip to content
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

SugaredLogger: Turn error into zap.Error #1185

Merged
merged 4 commits into from Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions sugar.go
Expand Up @@ -31,6 +31,7 @@ import (
const (
_oddNumberErrMsg = "Ignored key without a value."
_nonStringKeyErrMsg = "Ignored key-value pairs with non-string keys."
_multipleErrMsg = "Multiple errors without a key."
)

// A SugaredLogger wraps the base Logger functionality in a slower, but less
Expand Down Expand Up @@ -340,6 +341,7 @@ func (s *SugaredLogger) sweetenFields(args []interface{}) []Field {
// fields, we shouldn't penalize them with extra allocations.
fields := make([]Field, 0, len(args))
var invalid invalidPairs
var seenError bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's combine these into a var block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Thank you. Done.


for i := 0; i < len(args); {
// This is a strongly-typed field. Consume it and move on.
Expand All @@ -349,6 +351,18 @@ func (s *SugaredLogger) sweetenFields(args []interface{}) []Field {
continue
}

// If it is an error, consume it and move on.
if err, ok := args[i].(error); ok {
if !seenError {
seenError = true
fields = append(fields, Error(err))
} else {
s.base.Error(_multipleErrMsg, Error(err))
}
i++
continue
}

// Make sure this element isn't a dangling key.
if i == len(args)-1 {
s.base.Error(_oddNumberErrMsg, Any("ignored", args[i]))
Expand Down
21 changes: 19 additions & 2 deletions sugar_test.go
Expand Up @@ -21,6 +21,7 @@
package zap

import (
"errors"
"testing"

"go.uber.org/zap/internal/exit"
Expand All @@ -46,6 +47,12 @@ func TestSugarWith(t *testing.T) {
Context: []Field{Array("invalid", invalidPairs(pairs))},
}
}
ignoredError := func(err error) observer.LoggedEntry {
return observer.LoggedEntry{
Entry: zapcore.Entry{Level: ErrorLevel, Message: _multipleErrMsg},
Context: []Field{Error(err)},
}
}

tests := []struct {
desc string
Expand Down Expand Up @@ -122,6 +129,15 @@ func TestSugarWith(t *testing.T) {
nonString(invalidPair{2, true, "bar"}, invalidPair{5, 42, "reversed"}),
},
},
{
desc: "multiple errors",
args: []interface{}{errors.New("first"), errors.New("second"), errors.New("third")},
expected: []Field{Error(errors.New("first"))},
errLogs: []observer.LoggedEntry{
ignoredError(errors.New("second")),
ignoredError(errors.New("third")),
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -198,9 +214,10 @@ func TestSugarStructuredLogging(t *testing.T) {
}

// Common to all test cases.
err := errors.New("qux")
context := []interface{}{"foo", "bar"}
extra := []interface{}{"baz", false}
expectedFields := []Field{String("foo", "bar"), Bool("baz", false)}
extra := []interface{}{err, "baz", false}
expectedFields := []Field{String("foo", "bar"), Error(err), Bool("baz", false)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's combine these into a var block here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combined into a var block here too. Thank you!


for _, tt := range tests {
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
Expand Down