blob: 45f7b838dc1714a4d9e5fe63573d7231de3e33f2 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// Copyright (c) 2017 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21package zap
22
23import (
Abhay Kumara2ae5992025-11-10 14:02:24 +000024 "go.uber.org/zap/internal/pool"
khenaidooac637102019-01-14 15:44:34 -050025 "go.uber.org/zap/zapcore"
26)
27
Abhay Kumara2ae5992025-11-10 14:02:24 +000028var _errArrayElemPool = pool.New(func() *errArrayElem {
khenaidooac637102019-01-14 15:44:34 -050029 return &errArrayElem{}
Abhay Kumara2ae5992025-11-10 14:02:24 +000030})
khenaidooac637102019-01-14 15:44:34 -050031
32// Error is shorthand for the common idiom NamedError("error", err).
33func Error(err error) Field {
34 return NamedError("error", err)
35}
36
37// NamedError constructs a field that lazily stores err.Error() under the
38// provided key. Errors which also implement fmt.Formatter (like those produced
39// by github.com/pkg/errors) will also have their verbose representation stored
40// under key+"Verbose". If passed a nil error, the field is a no-op.
41//
42// For the common case in which the key is simply "error", the Error function
43// is shorter and less repetitive.
44func NamedError(key string, err error) Field {
45 if err == nil {
46 return Skip()
47 }
48 return Field{Key: key, Type: zapcore.ErrorType, Interface: err}
49}
50
51type errArray []error
52
53func (errs errArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
54 for i := range errs {
55 if errs[i] == nil {
56 continue
57 }
58 // To represent each error as an object with an "error" attribute and
59 // potentially an "errorVerbose" attribute, we need to wrap it in a
60 // type that implements LogObjectMarshaler. To prevent this from
61 // allocating, pool the wrapper type.
Abhay Kumara2ae5992025-11-10 14:02:24 +000062 elem := _errArrayElemPool.Get()
khenaidooac637102019-01-14 15:44:34 -050063 elem.error = errs[i]
Abhay Kumara2ae5992025-11-10 14:02:24 +000064 err := arr.AppendObject(elem)
khenaidooac637102019-01-14 15:44:34 -050065 elem.error = nil
66 _errArrayElemPool.Put(elem)
Abhay Kumara2ae5992025-11-10 14:02:24 +000067 if err != nil {
68 return err
69 }
khenaidooac637102019-01-14 15:44:34 -050070 }
71 return nil
72}
73
74type errArrayElem struct {
75 error
76}
77
78func (e *errArrayElem) MarshalLogObject(enc zapcore.ObjectEncoder) error {
79 // Re-use the error field's logic, which supports non-standard error types.
80 Error(e.error).AddTo(enc)
81 return nil
82}