Codebase list golang-go.uber-zap / 9b86a50
SugaredLogger: Turn error into zap.Error (#1185) Craig Pastro authored 1 year, 7 months ago GitHub committed 1 year, 7 months ago
2 changed file(s) with 43 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
3030 const (
3131 _oddNumberErrMsg = "Ignored key without a value."
3232 _nonStringKeyErrMsg = "Ignored key-value pairs with non-string keys."
33 _multipleErrMsg = "Multiple errors without a key."
3334 )
3435
3536 // A SugaredLogger wraps the base Logger functionality in a slower, but less
335336 return nil
336337 }
337338
338 // Allocate enough space for the worst case; if users pass only structured
339 // fields, we shouldn't penalize them with extra allocations.
340 fields := make([]Field, 0, len(args))
341 var invalid invalidPairs
339 var (
340 // Allocate enough space for the worst case; if users pass only structured
341 // fields, we shouldn't penalize them with extra allocations.
342 fields = make([]Field, 0, len(args))
343 invalid invalidPairs
344 seenError bool
345 )
342346
343347 for i := 0; i < len(args); {
344348 // This is a strongly-typed field. Consume it and move on.
345349 if f, ok := args[i].(Field); ok {
346350 fields = append(fields, f)
351 i++
352 continue
353 }
354
355 // If it is an error, consume it and move on.
356 if err, ok := args[i].(error); ok {
357 if !seenError {
358 seenError = true
359 fields = append(fields, Error(err))
360 } else {
361 s.base.Error(_multipleErrMsg, Error(err))
362 }
347363 i++
348364 continue
349365 }
2020 package zap
2121
2222 import (
23 "errors"
2324 "testing"
2425
2526 "go.uber.org/zap/internal/exit"
4546 Context: []Field{Array("invalid", invalidPairs(pairs))},
4647 }
4748 }
49 ignoredError := func(err error) observer.LoggedEntry {
50 return observer.LoggedEntry{
51 Entry: zapcore.Entry{Level: ErrorLevel, Message: _multipleErrMsg},
52 Context: []Field{Error(err)},
53 }
54 }
4855
4956 tests := []struct {
5057 desc string
119126 errLogs: []observer.LoggedEntry{
120127 ignored("dangling"),
121128 nonString(invalidPair{2, true, "bar"}, invalidPair{5, 42, "reversed"}),
129 },
130 },
131 {
132 desc: "multiple errors",
133 args: []interface{}{errors.New("first"), errors.New("second"), errors.New("third")},
134 expected: []Field{Error(errors.New("first"))},
135 errLogs: []observer.LoggedEntry{
136 ignoredError(errors.New("second")),
137 ignoredError(errors.New("third")),
122138 },
123139 },
124140 }
197213 }
198214
199215 // Common to all test cases.
200 context := []interface{}{"foo", "bar"}
201 extra := []interface{}{"baz", false}
202 expectedFields := []Field{String("foo", "bar"), Bool("baz", false)}
203
216 var (
217 err = errors.New("qux")
218 context = []interface{}{"foo", "bar"}
219 extra = []interface{}{err, "baz", false}
220 expectedFields = []Field{String("foo", "bar"), Error(err), Bool("baz", false)}
221 )
222
204223 for _, tt := range tests {
205224 withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
206225 logger.With(context...).Debugw(tt.msg, extra...)