Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 1 | // Copyright (c) 2016 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 | |
| 21 | package zap |
| 22 | |
| 23 | import ( |
| 24 | "time" |
| 25 | |
| 26 | "go.uber.org/zap/zapcore" |
| 27 | ) |
| 28 | |
| 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} |
| 34 | } |
| 35 | |
| 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)) |
| 39 | } |
| 40 | |
| 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)) |
| 45 | } |
| 46 | |
| 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)) |
| 50 | } |
| 51 | |
| 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)) |
| 55 | } |
| 56 | |
| 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)) |
| 60 | } |
| 61 | |
| 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)) |
| 65 | } |
| 66 | |
| 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)) |
| 70 | } |
| 71 | |
| 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)) |
| 75 | } |
| 76 | |
| 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)) |
| 80 | } |
| 81 | |
| 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)) |
| 85 | } |
| 86 | |
| 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)) |
| 90 | } |
| 91 | |
| 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)) |
| 95 | } |
| 96 | |
| 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)) |
| 100 | } |
| 101 | |
| 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)) |
| 105 | } |
| 106 | |
| 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)) |
| 110 | } |
| 111 | |
| 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)) |
| 115 | } |
| 116 | |
| 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)) |
| 120 | } |
| 121 | |
| 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)) |
| 125 | } |
| 126 | |
| 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)) |
| 130 | } |
| 131 | |
| 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)) |
| 135 | } |
| 136 | |
| 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)) |
| 140 | } |
| 141 | |
| 142 | type bools []bool |
| 143 | |
| 144 | func (bs bools) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 145 | for i := range bs { |
| 146 | arr.AppendBool(bs[i]) |
| 147 | } |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | type byteStringsArray [][]byte |
| 152 | |
| 153 | func (bss byteStringsArray) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 154 | for i := range bss { |
| 155 | arr.AppendByteString(bss[i]) |
| 156 | } |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | type complex128s []complex128 |
| 161 | |
| 162 | func (nums complex128s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 163 | for i := range nums { |
| 164 | arr.AppendComplex128(nums[i]) |
| 165 | } |
| 166 | return nil |
| 167 | } |
| 168 | |
| 169 | type complex64s []complex64 |
| 170 | |
| 171 | func (nums complex64s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 172 | for i := range nums { |
| 173 | arr.AppendComplex64(nums[i]) |
| 174 | } |
| 175 | return nil |
| 176 | } |
| 177 | |
| 178 | type durations []time.Duration |
| 179 | |
| 180 | func (ds durations) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 181 | for i := range ds { |
| 182 | arr.AppendDuration(ds[i]) |
| 183 | } |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | type float64s []float64 |
| 188 | |
| 189 | func (nums float64s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 190 | for i := range nums { |
| 191 | arr.AppendFloat64(nums[i]) |
| 192 | } |
| 193 | return nil |
| 194 | } |
| 195 | |
| 196 | type float32s []float32 |
| 197 | |
| 198 | func (nums float32s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 199 | for i := range nums { |
| 200 | arr.AppendFloat32(nums[i]) |
| 201 | } |
| 202 | return nil |
| 203 | } |
| 204 | |
| 205 | type ints []int |
| 206 | |
| 207 | func (nums ints) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 208 | for i := range nums { |
| 209 | arr.AppendInt(nums[i]) |
| 210 | } |
| 211 | return nil |
| 212 | } |
| 213 | |
| 214 | type int64s []int64 |
| 215 | |
| 216 | func (nums int64s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 217 | for i := range nums { |
| 218 | arr.AppendInt64(nums[i]) |
| 219 | } |
| 220 | return nil |
| 221 | } |
| 222 | |
| 223 | type int32s []int32 |
| 224 | |
| 225 | func (nums int32s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 226 | for i := range nums { |
| 227 | arr.AppendInt32(nums[i]) |
| 228 | } |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | type int16s []int16 |
| 233 | |
| 234 | func (nums int16s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 235 | for i := range nums { |
| 236 | arr.AppendInt16(nums[i]) |
| 237 | } |
| 238 | return nil |
| 239 | } |
| 240 | |
| 241 | type int8s []int8 |
| 242 | |
| 243 | func (nums int8s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 244 | for i := range nums { |
| 245 | arr.AppendInt8(nums[i]) |
| 246 | } |
| 247 | return nil |
| 248 | } |
| 249 | |
| 250 | type stringArray []string |
| 251 | |
| 252 | func (ss stringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 253 | for i := range ss { |
| 254 | arr.AppendString(ss[i]) |
| 255 | } |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | type times []time.Time |
| 260 | |
| 261 | func (ts times) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 262 | for i := range ts { |
| 263 | arr.AppendTime(ts[i]) |
| 264 | } |
| 265 | return nil |
| 266 | } |
| 267 | |
| 268 | type uints []uint |
| 269 | |
| 270 | func (nums uints) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 271 | for i := range nums { |
| 272 | arr.AppendUint(nums[i]) |
| 273 | } |
| 274 | return nil |
| 275 | } |
| 276 | |
| 277 | type uint64s []uint64 |
| 278 | |
| 279 | func (nums uint64s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 280 | for i := range nums { |
| 281 | arr.AppendUint64(nums[i]) |
| 282 | } |
| 283 | return nil |
| 284 | } |
| 285 | |
| 286 | type uint32s []uint32 |
| 287 | |
| 288 | func (nums uint32s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 289 | for i := range nums { |
| 290 | arr.AppendUint32(nums[i]) |
| 291 | } |
| 292 | return nil |
| 293 | } |
| 294 | |
| 295 | type uint16s []uint16 |
| 296 | |
| 297 | func (nums uint16s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 298 | for i := range nums { |
| 299 | arr.AppendUint16(nums[i]) |
| 300 | } |
| 301 | return nil |
| 302 | } |
| 303 | |
| 304 | type uint8s []uint8 |
| 305 | |
| 306 | func (nums uint8s) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 307 | for i := range nums { |
| 308 | arr.AppendUint8(nums[i]) |
| 309 | } |
| 310 | return nil |
| 311 | } |
| 312 | |
| 313 | type uintptrs []uintptr |
| 314 | |
| 315 | func (nums uintptrs) MarshalLogArray(arr zapcore.ArrayEncoder) error { |
| 316 | for i := range nums { |
| 317 | arr.AppendUintptr(nums[i]) |
| 318 | } |
| 319 | return nil |
| 320 | } |