field.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. // nilField returns a field which will marshal explicitly as nil. See motivation
  36. // in https://github.com/uber-go/zap/issues/753 . If we ever make breaking
  37. // changes and add zapcore.NilType and zapcore.ObjectEncoder.AddNil, the
  38. // implementation here should be changed to reflect that.
  39. func nilField(key string) Field { return Reflect(key, nil) }
  40. // Binary constructs a field that carries an opaque binary blob.
  41. //
  42. // Binary data is serialized in an encoding-appropriate format. For example,
  43. // zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text,
  44. // use ByteString.
  45. func Binary(key string, val []byte) Field {
  46. return Field{Key: key, Type: zapcore.BinaryType, Interface: val}
  47. }
  48. // Bool constructs a field that carries a bool.
  49. func Bool(key string, val bool) Field {
  50. var ival int64
  51. if val {
  52. ival = 1
  53. }
  54. return Field{Key: key, Type: zapcore.BoolType, Integer: ival}
  55. }
  56. // Boolp constructs a field that carries a *bool. The returned Field will safely
  57. // and explicitly represent `nil` when appropriate.
  58. func Boolp(key string, val *bool) Field {
  59. if val == nil {
  60. return nilField(key)
  61. }
  62. return Bool(key, *val)
  63. }
  64. // ByteString constructs a field that carries UTF-8 encoded text as a []byte.
  65. // To log opaque binary blobs (which aren't necessarily valid UTF-8), use
  66. // Binary.
  67. func ByteString(key string, val []byte) Field {
  68. return Field{Key: key, Type: zapcore.ByteStringType, Interface: val}
  69. }
  70. // Complex128 constructs a field that carries a complex number. Unlike most
  71. // numeric fields, this costs an allocation (to convert the complex128 to
  72. // interface{}).
  73. func Complex128(key string, val complex128) Field {
  74. return Field{Key: key, Type: zapcore.Complex128Type, Interface: val}
  75. }
  76. // Complex128p constructs a field that carries a *complex128. The returned Field will safely
  77. // and explicitly represent `nil` when appropriate.
  78. func Complex128p(key string, val *complex128) Field {
  79. if val == nil {
  80. return nilField(key)
  81. }
  82. return Complex128(key, *val)
  83. }
  84. // Complex64 constructs a field that carries a complex number. Unlike most
  85. // numeric fields, this costs an allocation (to convert the complex64 to
  86. // interface{}).
  87. func Complex64(key string, val complex64) Field {
  88. return Field{Key: key, Type: zapcore.Complex64Type, Interface: val}
  89. }
  90. // Complex64p constructs a field that carries a *complex64. The returned Field will safely
  91. // and explicitly represent `nil` when appropriate.
  92. func Complex64p(key string, val *complex64) Field {
  93. if val == nil {
  94. return nilField(key)
  95. }
  96. return Complex64(key, *val)
  97. }
  98. // Float64 constructs a field that carries a float64. The way the
  99. // floating-point value is represented is encoder-dependent, so marshaling is
  100. // necessarily lazy.
  101. func Float64(key string, val float64) Field {
  102. return Field{Key: key, Type: zapcore.Float64Type, Integer: int64(math.Float64bits(val))}
  103. }
  104. // Float64p constructs a field that carries a *float64. The returned Field will safely
  105. // and explicitly represent `nil` when appropriate.
  106. func Float64p(key string, val *float64) Field {
  107. if val == nil {
  108. return nilField(key)
  109. }
  110. return Float64(key, *val)
  111. }
  112. // Float32 constructs a field that carries a float32. The way the
  113. // floating-point value is represented is encoder-dependent, so marshaling is
  114. // necessarily lazy.
  115. func Float32(key string, val float32) Field {
  116. return Field{Key: key, Type: zapcore.Float32Type, Integer: int64(math.Float32bits(val))}
  117. }
  118. // Float32p constructs a field that carries a *float32. The returned Field will safely
  119. // and explicitly represent `nil` when appropriate.
  120. func Float32p(key string, val *float32) Field {
  121. if val == nil {
  122. return nilField(key)
  123. }
  124. return Float32(key, *val)
  125. }
  126. // Int constructs a field with the given key and value.
  127. func Int(key string, val int) Field {
  128. return Int64(key, int64(val))
  129. }
  130. // Intp constructs a field that carries a *int. The returned Field will safely
  131. // and explicitly represent `nil` when appropriate.
  132. func Intp(key string, val *int) Field {
  133. if val == nil {
  134. return nilField(key)
  135. }
  136. return Int(key, *val)
  137. }
  138. // Int64 constructs a field with the given key and value.
  139. func Int64(key string, val int64) Field {
  140. return Field{Key: key, Type: zapcore.Int64Type, Integer: val}
  141. }
  142. // Int64p constructs a field that carries a *int64. The returned Field will safely
  143. // and explicitly represent `nil` when appropriate.
  144. func Int64p(key string, val *int64) Field {
  145. if val == nil {
  146. return nilField(key)
  147. }
  148. return Int64(key, *val)
  149. }
  150. // Int32 constructs a field with the given key and value.
  151. func Int32(key string, val int32) Field {
  152. return Field{Key: key, Type: zapcore.Int32Type, Integer: int64(val)}
  153. }
  154. // Int32p constructs a field that carries a *int32. The returned Field will safely
  155. // and explicitly represent `nil` when appropriate.
  156. func Int32p(key string, val *int32) Field {
  157. if val == nil {
  158. return nilField(key)
  159. }
  160. return Int32(key, *val)
  161. }
  162. // Int16 constructs a field with the given key and value.
  163. func Int16(key string, val int16) Field {
  164. return Field{Key: key, Type: zapcore.Int16Type, Integer: int64(val)}
  165. }
  166. // Int16p constructs a field that carries a *int16. The returned Field will safely
  167. // and explicitly represent `nil` when appropriate.
  168. func Int16p(key string, val *int16) Field {
  169. if val == nil {
  170. return nilField(key)
  171. }
  172. return Int16(key, *val)
  173. }
  174. // Int8 constructs a field with the given key and value.
  175. func Int8(key string, val int8) Field {
  176. return Field{Key: key, Type: zapcore.Int8Type, Integer: int64(val)}
  177. }
  178. // Int8p constructs a field that carries a *int8. The returned Field will safely
  179. // and explicitly represent `nil` when appropriate.
  180. func Int8p(key string, val *int8) Field {
  181. if val == nil {
  182. return nilField(key)
  183. }
  184. return Int8(key, *val)
  185. }
  186. // String constructs a field with the given key and value.
  187. func String(key string, val string) Field {
  188. return Field{Key: key, Type: zapcore.StringType, String: val}
  189. }
  190. // Stringp constructs a field that carries a *string. The returned Field will safely
  191. // and explicitly represent `nil` when appropriate.
  192. func Stringp(key string, val *string) Field {
  193. if val == nil {
  194. return nilField(key)
  195. }
  196. return String(key, *val)
  197. }
  198. // Uint constructs a field with the given key and value.
  199. func Uint(key string, val uint) Field {
  200. return Uint64(key, uint64(val))
  201. }
  202. // Uintp constructs a field that carries a *uint. The returned Field will safely
  203. // and explicitly represent `nil` when appropriate.
  204. func Uintp(key string, val *uint) Field {
  205. if val == nil {
  206. return nilField(key)
  207. }
  208. return Uint(key, *val)
  209. }
  210. // Uint64 constructs a field with the given key and value.
  211. func Uint64(key string, val uint64) Field {
  212. return Field{Key: key, Type: zapcore.Uint64Type, Integer: int64(val)}
  213. }
  214. // Uint64p constructs a field that carries a *uint64. The returned Field will safely
  215. // and explicitly represent `nil` when appropriate.
  216. func Uint64p(key string, val *uint64) Field {
  217. if val == nil {
  218. return nilField(key)
  219. }
  220. return Uint64(key, *val)
  221. }
  222. // Uint32 constructs a field with the given key and value.
  223. func Uint32(key string, val uint32) Field {
  224. return Field{Key: key, Type: zapcore.Uint32Type, Integer: int64(val)}
  225. }
  226. // Uint32p constructs a field that carries a *uint32. The returned Field will safely
  227. // and explicitly represent `nil` when appropriate.
  228. func Uint32p(key string, val *uint32) Field {
  229. if val == nil {
  230. return nilField(key)
  231. }
  232. return Uint32(key, *val)
  233. }
  234. // Uint16 constructs a field with the given key and value.
  235. func Uint16(key string, val uint16) Field {
  236. return Field{Key: key, Type: zapcore.Uint16Type, Integer: int64(val)}
  237. }
  238. // Uint16p constructs a field that carries a *uint16. The returned Field will safely
  239. // and explicitly represent `nil` when appropriate.
  240. func Uint16p(key string, val *uint16) Field {
  241. if val == nil {
  242. return nilField(key)
  243. }
  244. return Uint16(key, *val)
  245. }
  246. // Uint8 constructs a field with the given key and value.
  247. func Uint8(key string, val uint8) Field {
  248. return Field{Key: key, Type: zapcore.Uint8Type, Integer: int64(val)}
  249. }
  250. // Uint8p constructs a field that carries a *uint8. The returned Field will safely
  251. // and explicitly represent `nil` when appropriate.
  252. func Uint8p(key string, val *uint8) Field {
  253. if val == nil {
  254. return nilField(key)
  255. }
  256. return Uint8(key, *val)
  257. }
  258. // Uintptr constructs a field with the given key and value.
  259. func Uintptr(key string, val uintptr) Field {
  260. return Field{Key: key, Type: zapcore.UintptrType, Integer: int64(val)}
  261. }
  262. // Uintptrp constructs a field that carries a *uintptr. The returned Field will safely
  263. // and explicitly represent `nil` when appropriate.
  264. func Uintptrp(key string, val *uintptr) Field {
  265. if val == nil {
  266. return nilField(key)
  267. }
  268. return Uintptr(key, *val)
  269. }
  270. // Reflect constructs a field with the given key and an arbitrary object. It uses
  271. // an encoding-appropriate, reflection-based function to lazily serialize nearly
  272. // any object into the logging context, but it's relatively slow and
  273. // allocation-heavy. Outside tests, Any is always a better choice.
  274. //
  275. // If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect
  276. // includes the error message in the final log output.
  277. func Reflect(key string, val interface{}) Field {
  278. return Field{Key: key, Type: zapcore.ReflectType, Interface: val}
  279. }
  280. // Namespace creates a named, isolated scope within the logger's context. All
  281. // subsequent fields will be added to the new namespace.
  282. //
  283. // This helps prevent key collisions when injecting loggers into sub-components
  284. // or third-party libraries.
  285. func Namespace(key string) Field {
  286. return Field{Key: key, Type: zapcore.NamespaceType}
  287. }
  288. // Stringer constructs a field with the given key and the output of the value's
  289. // String method. The Stringer's String method is called lazily.
  290. func Stringer(key string, val fmt.Stringer) Field {
  291. return Field{Key: key, Type: zapcore.StringerType, Interface: val}
  292. }
  293. // Time constructs a Field with the given key and value. The encoder
  294. // controls how the time is serialized.
  295. func Time(key string, val time.Time) Field {
  296. return Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano(), Interface: val.Location()}
  297. }
  298. // Timep constructs a field that carries a *time.Time. The returned Field will safely
  299. // and explicitly represent `nil` when appropriate.
  300. func Timep(key string, val *time.Time) Field {
  301. if val == nil {
  302. return nilField(key)
  303. }
  304. return Time(key, *val)
  305. }
  306. // Stack constructs a field that stores a stacktrace of the current goroutine
  307. // under provided key. Keep in mind that taking a stacktrace is eager and
  308. // expensive (relatively speaking); this function both makes an allocation and
  309. // takes about two microseconds.
  310. func Stack(key string) Field {
  311. // Returning the stacktrace as a string costs an allocation, but saves us
  312. // from expanding the zapcore.Field union struct to include a byte slice. Since
  313. // taking a stacktrace is already so expensive (~10us), the extra allocation
  314. // is okay.
  315. return String(key, takeStacktrace())
  316. }
  317. // Duration constructs a field with the given key and value. The encoder
  318. // controls how the duration is serialized.
  319. func Duration(key string, val time.Duration) Field {
  320. return Field{Key: key, Type: zapcore.DurationType, Integer: int64(val)}
  321. }
  322. // Durationp constructs a field that carries a *time.Duration. The returned Field will safely
  323. // and explicitly represent `nil` when appropriate.
  324. func Durationp(key string, val *time.Duration) Field {
  325. if val == nil {
  326. return nilField(key)
  327. }
  328. return Duration(key, *val)
  329. }
  330. // Object constructs a field with the given key and ObjectMarshaler. It
  331. // provides a flexible, but still type-safe and efficient, way to add map- or
  332. // struct-like user-defined types to the logging context. The struct's
  333. // MarshalLogObject method is called lazily.
  334. func Object(key string, val zapcore.ObjectMarshaler) Field {
  335. return Field{Key: key, Type: zapcore.ObjectMarshalerType, Interface: val}
  336. }
  337. // Any takes a key and an arbitrary value and chooses the best way to represent
  338. // them as a field, falling back to a reflection-based approach only if
  339. // necessary.
  340. //
  341. // Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between
  342. // them. To minimize surprises, []byte values are treated as binary blobs, byte
  343. // values are treated as uint8, and runes are always treated as integers.
  344. func Any(key string, value interface{}) Field {
  345. switch val := value.(type) {
  346. case zapcore.ObjectMarshaler:
  347. return Object(key, val)
  348. case zapcore.ArrayMarshaler:
  349. return Array(key, val)
  350. case bool:
  351. return Bool(key, val)
  352. case *bool:
  353. return Boolp(key, val)
  354. case []bool:
  355. return Bools(key, val)
  356. case complex128:
  357. return Complex128(key, val)
  358. case *complex128:
  359. return Complex128p(key, val)
  360. case []complex128:
  361. return Complex128s(key, val)
  362. case complex64:
  363. return Complex64(key, val)
  364. case *complex64:
  365. return Complex64p(key, val)
  366. case []complex64:
  367. return Complex64s(key, val)
  368. case float64:
  369. return Float64(key, val)
  370. case *float64:
  371. return Float64p(key, val)
  372. case []float64:
  373. return Float64s(key, val)
  374. case float32:
  375. return Float32(key, val)
  376. case *float32:
  377. return Float32p(key, val)
  378. case []float32:
  379. return Float32s(key, val)
  380. case int:
  381. return Int(key, val)
  382. case *int:
  383. return Intp(key, val)
  384. case []int:
  385. return Ints(key, val)
  386. case int64:
  387. return Int64(key, val)
  388. case *int64:
  389. return Int64p(key, val)
  390. case []int64:
  391. return Int64s(key, val)
  392. case int32:
  393. return Int32(key, val)
  394. case *int32:
  395. return Int32p(key, val)
  396. case []int32:
  397. return Int32s(key, val)
  398. case int16:
  399. return Int16(key, val)
  400. case *int16:
  401. return Int16p(key, val)
  402. case []int16:
  403. return Int16s(key, val)
  404. case int8:
  405. return Int8(key, val)
  406. case *int8:
  407. return Int8p(key, val)
  408. case []int8:
  409. return Int8s(key, val)
  410. case string:
  411. return String(key, val)
  412. case *string:
  413. return Stringp(key, val)
  414. case []string:
  415. return Strings(key, val)
  416. case uint:
  417. return Uint(key, val)
  418. case *uint:
  419. return Uintp(key, val)
  420. case []uint:
  421. return Uints(key, val)
  422. case uint64:
  423. return Uint64(key, val)
  424. case *uint64:
  425. return Uint64p(key, val)
  426. case []uint64:
  427. return Uint64s(key, val)
  428. case uint32:
  429. return Uint32(key, val)
  430. case *uint32:
  431. return Uint32p(key, val)
  432. case []uint32:
  433. return Uint32s(key, val)
  434. case uint16:
  435. return Uint16(key, val)
  436. case *uint16:
  437. return Uint16p(key, val)
  438. case []uint16:
  439. return Uint16s(key, val)
  440. case uint8:
  441. return Uint8(key, val)
  442. case *uint8:
  443. return Uint8p(key, val)
  444. case []byte:
  445. return Binary(key, val)
  446. case uintptr:
  447. return Uintptr(key, val)
  448. case *uintptr:
  449. return Uintptrp(key, val)
  450. case []uintptr:
  451. return Uintptrs(key, val)
  452. case time.Time:
  453. return Time(key, val)
  454. case *time.Time:
  455. return Timep(key, val)
  456. case []time.Time:
  457. return Times(key, val)
  458. case time.Duration:
  459. return Duration(key, val)
  460. case *time.Duration:
  461. return Durationp(key, val)
  462. case []time.Duration:
  463. return Durations(key, val)
  464. case error:
  465. return NamedError(key, val)
  466. case []error:
  467. return Errors(key, val)
  468. case fmt.Stringer:
  469. return Stringer(key, val)
  470. default:
  471. return Reflect(key, val)
  472. }
  473. }