field.go 10 KB

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