field.go 11 KB

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