field.go 16 KB

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