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 21package zap 22 23import ( 24 "fmt" 25 "math" 26 "time" 27 28 "go.uber.org/zap/zapcore" 29) 30 31// Skip constructs a no-op field, which is often useful when handling invalid 32// inputs in other Field constructors. 33func Skip() zapcore.Field { 34 return zapcore.Field{Type: zapcore.SkipType} 35} 36 37// Binary constructs a field that carries an opaque binary blob. 38// 39// Binary data is serialized in an encoding-appropriate format. For example, 40// zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text, 41// use ByteString. 42func Binary(key string, val []byte) zapcore.Field { 43 return zapcore.Field{Key: key, Type: zapcore.BinaryType, Interface: val} 44} 45 46// Bool constructs a field that carries a bool. 47func Bool(key string, val bool) zapcore.Field { 48 var ival int64 49 if val { 50 ival = 1 51 } 52 return zapcore.Field{Key: key, Type: zapcore.BoolType, Integer: ival} 53} 54 55// ByteString constructs a field that carries UTF-8 encoded text as a []byte. 56// To log opaque binary blobs (which aren't necessarily valid UTF-8), use 57// Binary. 58func ByteString(key string, val []byte) zapcore.Field { 59 return zapcore.Field{Key: key, Type: zapcore.ByteStringType, Interface: val} 60} 61 62// Complex128 constructs a field that carries a complex number. Unlike most 63// numeric fields, this costs an allocation (to convert the complex128 to 64// interface{}). 65func Complex128(key string, val complex128) zapcore.Field { 66 return zapcore.Field{Key: key, Type: zapcore.Complex128Type, Interface: val} 67} 68 69// Complex64 constructs a field that carries a complex number. Unlike most 70// numeric fields, this costs an allocation (to convert the complex64 to 71// interface{}). 72func Complex64(key string, val complex64) zapcore.Field { 73 return zapcore.Field{Key: key, Type: zapcore.Complex64Type, Interface: val} 74} 75 76// Float64 constructs a field that carries a float64. The way the 77// floating-point value is represented is encoder-dependent, so marshaling is 78// necessarily lazy. 79func Float64(key string, val float64) zapcore.Field { 80 return zapcore.Field{Key: key, Type: zapcore.Float64Type, Integer: int64(math.Float64bits(val))} 81} 82 83// Float32 constructs a field that carries a float32. The way the 84// floating-point value is represented is encoder-dependent, so marshaling is 85// necessarily lazy. 86func Float32(key string, val float32) zapcore.Field { 87 return zapcore.Field{Key: key, Type: zapcore.Float32Type, Integer: int64(math.Float32bits(val))} 88} 89 90// Int constructs a field with the given key and value. 91func Int(key string, val int) zapcore.Field { 92 return Int64(key, int64(val)) 93} 94 95// Int64 constructs a field with the given key and value. 96func Int64(key string, val int64) zapcore.Field { 97 return zapcore.Field{Key: key, Type: zapcore.Int64Type, Integer: val} 98} 99 100// Int32 constructs a field with the given key and value. 101func Int32(key string, val int32) zapcore.Field { 102 return zapcore.Field{Key: key, Type: zapcore.Int32Type, Integer: int64(val)} 103} 104 105// Int16 constructs a field with the given key and value. 106func Int16(key string, val int16) zapcore.Field { 107 return zapcore.Field{Key: key, Type: zapcore.Int16Type, Integer: int64(val)} 108} 109 110// Int8 constructs a field with the given key and value. 111func Int8(key string, val int8) zapcore.Field { 112 return zapcore.Field{Key: key, Type: zapcore.Int8Type, Integer: int64(val)} 113} 114 115// String constructs a field with the given key and value. 116func String(key string, val string) zapcore.Field { 117 return zapcore.Field{Key: key, Type: zapcore.StringType, String: val} 118} 119 120// Uint constructs a field with the given key and value. 121func Uint(key string, val uint) zapcore.Field { 122 return Uint64(key, uint64(val)) 123} 124 125// Uint64 constructs a field with the given key and value. 126func Uint64(key string, val uint64) zapcore.Field { 127 return zapcore.Field{Key: key, Type: zapcore.Uint64Type, Integer: int64(val)} 128} 129 130// Uint32 constructs a field with the given key and value. 131func Uint32(key string, val uint32) zapcore.Field { 132 return zapcore.Field{Key: key, Type: zapcore.Uint32Type, Integer: int64(val)} 133} 134 135// Uint16 constructs a field with the given key and value. 136func Uint16(key string, val uint16) zapcore.Field { 137 return zapcore.Field{Key: key, Type: zapcore.Uint16Type, Integer: int64(val)} 138} 139 140// Uint8 constructs a field with the given key and value. 141func Uint8(key string, val uint8) zapcore.Field { 142 return zapcore.Field{Key: key, Type: zapcore.Uint8Type, Integer: int64(val)} 143} 144 145// Uintptr constructs a field with the given key and value. 146func Uintptr(key string, val uintptr) zapcore.Field { 147 return zapcore.Field{Key: key, Type: zapcore.UintptrType, Integer: int64(val)} 148} 149 150// Reflect constructs a field with the given key and an arbitrary object. It uses 151// an encoding-appropriate, reflection-based function to lazily serialize nearly 152// any object into the logging context, but it's relatively slow and 153// allocation-heavy. Outside tests, Any is always a better choice. 154// 155// If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect 156// includes the error message in the final log output. 157func Reflect(key string, val interface{}) zapcore.Field { 158 return zapcore.Field{Key: key, Type: zapcore.ReflectType, Interface: val} 159} 160 161// Namespace creates a named, isolated scope within the logger's context. All 162// subsequent fields will be added to the new namespace. 163// 164// This helps prevent key collisions when injecting loggers into sub-components 165// or third-party libraries. 166func Namespace(key string) zapcore.Field { 167 return zapcore.Field{Key: key, Type: zapcore.NamespaceType} 168} 169 170// Stringer constructs a field with the given key and the output of the value's 171// String method. The Stringer's String method is called lazily. 172func Stringer(key string, val fmt.Stringer) zapcore.Field { 173 return zapcore.Field{Key: key, Type: zapcore.StringerType, Interface: val} 174} 175 176// Time constructs a zapcore.Field with the given key and value. The encoder 177// controls how the time is serialized. 178func Time(key string, val time.Time) zapcore.Field { 179 return zapcore.Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano(), Interface: val.Location()} 180} 181 182// Stack constructs a field that stores a stacktrace of the current goroutine 183// under provided key. Keep in mind that taking a stacktrace is eager and 184// expensive (relatively speaking); this function both makes an allocation and 185// takes about two microseconds. 186func Stack(key string) zapcore.Field { 187 // Returning the stacktrace as a string costs an allocation, but saves us 188 // from expanding the zapcore.Field union struct to include a byte slice. Since 189 // taking a stacktrace is already so expensive (~10us), the extra allocation 190 // is okay. 191 return String(key, takeStacktrace()) 192} 193 194// Duration constructs a field with the given key and value. The encoder 195// controls how the duration is serialized. 196func Duration(key string, val time.Duration) zapcore.Field { 197 return zapcore.Field{Key: key, Type: zapcore.DurationType, Integer: int64(val)} 198} 199 200// Object constructs a field with the given key and ObjectMarshaler. It 201// provides a flexible, but still type-safe and efficient, way to add map- or 202// struct-like user-defined types to the logging context. The struct's 203// MarshalLogObject method is called lazily. 204func Object(key string, val zapcore.ObjectMarshaler) zapcore.Field { 205 return zapcore.Field{Key: key, Type: zapcore.ObjectMarshalerType, Interface: val} 206} 207 208// Any takes a key and an arbitrary value and chooses the best way to represent 209// them as a field, falling back to a reflection-based approach only if 210// necessary. 211// 212// Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between 213// them. To minimize suprise, []byte values are treated as binary blobs, byte 214// values are treated as uint8, and runes are always treated as integers. 215func Any(key string, value interface{}) zapcore.Field { 216 switch val := value.(type) { 217 case zapcore.ObjectMarshaler: 218 return Object(key, val) 219 case zapcore.ArrayMarshaler: 220 return Array(key, val) 221 case bool: 222 return Bool(key, val) 223 case []bool: 224 return Bools(key, val) 225 case complex128: 226 return Complex128(key, val) 227 case []complex128: 228 return Complex128s(key, val) 229 case complex64: 230 return Complex64(key, val) 231 case []complex64: 232 return Complex64s(key, val) 233 case float64: 234 return Float64(key, val) 235 case []float64: 236 return Float64s(key, val) 237 case float32: 238 return Float32(key, val) 239 case []float32: 240 return Float32s(key, val) 241 case int: 242 return Int(key, val) 243 case []int: 244 return Ints(key, val) 245 case int64: 246 return Int64(key, val) 247 case []int64: 248 return Int64s(key, val) 249 case int32: 250 return Int32(key, val) 251 case []int32: 252 return Int32s(key, val) 253 case int16: 254 return Int16(key, val) 255 case []int16: 256 return Int16s(key, val) 257 case int8: 258 return Int8(key, val) 259 case []int8: 260 return Int8s(key, val) 261 case string: 262 return String(key, val) 263 case []string: 264 return Strings(key, val) 265 case uint: 266 return Uint(key, val) 267 case []uint: 268 return Uints(key, val) 269 case uint64: 270 return Uint64(key, val) 271 case []uint64: 272 return Uint64s(key, val) 273 case uint32: 274 return Uint32(key, val) 275 case []uint32: 276 return Uint32s(key, val) 277 case uint16: 278 return Uint16(key, val) 279 case []uint16: 280 return Uint16s(key, val) 281 case uint8: 282 return Uint8(key, val) 283 case []byte: 284 return Binary(key, val) 285 case uintptr: 286 return Uintptr(key, val) 287 case []uintptr: 288 return Uintptrs(key, val) 289 case time.Time: 290 return Time(key, val) 291 case []time.Time: 292 return Times(key, val) 293 case time.Duration: 294 return Duration(key, val) 295 case []time.Duration: 296 return Durations(key, val) 297 case error: 298 return NamedError(key, val) 299 case []error: 300 return Errors(key, val) 301 case fmt.Stringer: 302 return Stringer(key, val) 303 default: 304 return Reflect(key, val) 305 } 306} 307