1 // Copyright (c) 2016 Uber Technologies, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
26 "go.uber.org/zap/zapcore"
29 // Array constructs a field with the given key and ArrayMarshaler. It provides
30 // a flexible, but still type-safe and efficient, way to add array-like types
31 // to the logging context. The struct's MarshalLogArray method is called lazily.
32 func Array(key string, val zapcore.ArrayMarshaler) Field {
33 return Field{Key: key, Type: zapcore.ArrayMarshalerType, Interface: val}
36 // Bools constructs a field that carries a slice of bools.
37 func Bools(key string, bs []bool) Field {
38 return Array(key, bools(bs))
41 // ByteStrings constructs a field that carries a slice of []byte, each of which
42 // must be UTF-8 encoded text.
43 func ByteStrings(key string, bss [][]byte) Field {
44 return Array(key, byteStringsArray(bss))
47 // Complex128s constructs a field that carries a slice of complex numbers.
48 func Complex128s(key string, nums []complex128) Field {
49 return Array(key, complex128s(nums))
52 // Complex64s constructs a field that carries a slice of complex numbers.
53 func Complex64s(key string, nums []complex64) Field {
54 return Array(key, complex64s(nums))
57 // Durations constructs a field that carries a slice of time.Durations.
58 func Durations(key string, ds []time.Duration) Field {
59 return Array(key, durations(ds))
62 // Float64s constructs a field that carries a slice of floats.
63 func Float64s(key string, nums []float64) Field {
64 return Array(key, float64s(nums))
67 // Float32s constructs a field that carries a slice of floats.
68 func Float32s(key string, nums []float32) Field {
69 return Array(key, float32s(nums))
72 // Ints constructs a field that carries a slice of integers.
73 func Ints(key string, nums []int) Field {
74 return Array(key, ints(nums))
77 // Int64s constructs a field that carries a slice of integers.
78 func Int64s(key string, nums []int64) Field {
79 return Array(key, int64s(nums))
82 // Int32s constructs a field that carries a slice of integers.
83 func Int32s(key string, nums []int32) Field {
84 return Array(key, int32s(nums))
87 // Int16s constructs a field that carries a slice of integers.
88 func Int16s(key string, nums []int16) Field {
89 return Array(key, int16s(nums))
92 // Int8s constructs a field that carries a slice of integers.
93 func Int8s(key string, nums []int8) Field {
94 return Array(key, int8s(nums))
97 // Strings constructs a field that carries a slice of strings.
98 func Strings(key string, ss []string) Field {
99 return Array(key, stringArray(ss))
102 // Times constructs a field that carries a slice of time.Times.
103 func Times(key string, ts []time.Time) Field {
104 return Array(key, times(ts))
107 // Uints constructs a field that carries a slice of unsigned integers.
108 func Uints(key string, nums []uint) Field {
109 return Array(key, uints(nums))
112 // Uint64s constructs a field that carries a slice of unsigned integers.
113 func Uint64s(key string, nums []uint64) Field {
114 return Array(key, uint64s(nums))
117 // Uint32s constructs a field that carries a slice of unsigned integers.
118 func Uint32s(key string, nums []uint32) Field {
119 return Array(key, uint32s(nums))
122 // Uint16s constructs a field that carries a slice of unsigned integers.
123 func Uint16s(key string, nums []uint16) Field {
124 return Array(key, uint16s(nums))
127 // Uint8s constructs a field that carries a slice of unsigned integers.
128 func Uint8s(key string, nums []uint8) Field {
129 return Array(key, uint8s(nums))
132 // Uintptrs constructs a field that carries a slice of pointer addresses.
133 func Uintptrs(key string, us []uintptr) Field {
134 return Array(key, uintptrs(us))
137 // Errors constructs a field that carries a slice of errors.
138 func Errors(key string, errs []error) Field {
139 return Array(key, errArray(errs))
144 func (bs bools) MarshalLogArray(arr zapcore.ArrayEncoder) error {
146 arr.AppendBool(bs[i])
151 type byteStringsArray [][]byte
153 func (bss byteStringsArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
155 arr.AppendByteString(bss[i])
160 type complex128s []complex128
162 func (nums complex128s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
163 for i := range nums {
164 arr.AppendComplex128(nums[i])
169 type complex64s []complex64
171 func (nums complex64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
172 for i := range nums {
173 arr.AppendComplex64(nums[i])
178 type durations []time.Duration
180 func (ds durations) MarshalLogArray(arr zapcore.ArrayEncoder) error {
182 arr.AppendDuration(ds[i])
187 type float64s []float64
189 func (nums float64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
190 for i := range nums {
191 arr.AppendFloat64(nums[i])
196 type float32s []float32
198 func (nums float32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
199 for i := range nums {
200 arr.AppendFloat32(nums[i])
207 func (nums ints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
208 for i := range nums {
209 arr.AppendInt(nums[i])
216 func (nums int64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
217 for i := range nums {
218 arr.AppendInt64(nums[i])
225 func (nums int32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
226 for i := range nums {
227 arr.AppendInt32(nums[i])
234 func (nums int16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
235 for i := range nums {
236 arr.AppendInt16(nums[i])
243 func (nums int8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
244 for i := range nums {
245 arr.AppendInt8(nums[i])
250 type stringArray []string
252 func (ss stringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
254 arr.AppendString(ss[i])
259 type times []time.Time
261 func (ts times) MarshalLogArray(arr zapcore.ArrayEncoder) error {
263 arr.AppendTime(ts[i])
270 func (nums uints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
271 for i := range nums {
272 arr.AppendUint(nums[i])
277 type uint64s []uint64
279 func (nums uint64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
280 for i := range nums {
281 arr.AppendUint64(nums[i])
286 type uint32s []uint32
288 func (nums uint32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
289 for i := range nums {
290 arr.AppendUint32(nums[i])
295 type uint16s []uint16
297 func (nums uint16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
298 for i := range nums {
299 arr.AppendUint16(nums[i])
306 func (nums uint8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
307 for i := range nums {
308 arr.AppendUint8(nums[i])
313 type uintptrs []uintptr
315 func (nums uintptrs) MarshalLogArray(arr zapcore.ArrayEncoder) error {
316 for i := range nums {
317 arr.AppendUintptr(nums[i])