array.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. "time"
  24. "go.uber.org/zap/zapcore"
  25. )
  26. // Array constructs a field with the given key and ArrayMarshaler. It provides
  27. // a flexible, but still type-safe and efficient, way to add array-like types
  28. // to the logging context. The struct's MarshalLogArray method is called lazily.
  29. func Array(key string, val zapcore.ArrayMarshaler) Field {
  30. return Field{Key: key, Type: zapcore.ArrayMarshalerType, Interface: val}
  31. }
  32. // Bools constructs a field that carries a slice of bools.
  33. func Bools(key string, bs []bool) Field {
  34. return Array(key, bools(bs))
  35. }
  36. // ByteStrings constructs a field that carries a slice of []byte, each of which
  37. // must be UTF-8 encoded text.
  38. func ByteStrings(key string, bss [][]byte) Field {
  39. return Array(key, byteStringsArray(bss))
  40. }
  41. // Complex128s constructs a field that carries a slice of complex numbers.
  42. func Complex128s(key string, nums []complex128) Field {
  43. return Array(key, complex128s(nums))
  44. }
  45. // Complex64s constructs a field that carries a slice of complex numbers.
  46. func Complex64s(key string, nums []complex64) Field {
  47. return Array(key, complex64s(nums))
  48. }
  49. // Durations constructs a field that carries a slice of time.Durations.
  50. func Durations(key string, ds []time.Duration) Field {
  51. return Array(key, durations(ds))
  52. }
  53. // Float64s constructs a field that carries a slice of floats.
  54. func Float64s(key string, nums []float64) Field {
  55. return Array(key, float64s(nums))
  56. }
  57. // Float32s constructs a field that carries a slice of floats.
  58. func Float32s(key string, nums []float32) Field {
  59. return Array(key, float32s(nums))
  60. }
  61. // Ints constructs a field that carries a slice of integers.
  62. func Ints(key string, nums []int) Field {
  63. return Array(key, ints(nums))
  64. }
  65. // Int64s constructs a field that carries a slice of integers.
  66. func Int64s(key string, nums []int64) Field {
  67. return Array(key, int64s(nums))
  68. }
  69. // Int32s constructs a field that carries a slice of integers.
  70. func Int32s(key string, nums []int32) Field {
  71. return Array(key, int32s(nums))
  72. }
  73. // Int16s constructs a field that carries a slice of integers.
  74. func Int16s(key string, nums []int16) Field {
  75. return Array(key, int16s(nums))
  76. }
  77. // Int8s constructs a field that carries a slice of integers.
  78. func Int8s(key string, nums []int8) Field {
  79. return Array(key, int8s(nums))
  80. }
  81. // Objects constructs a field with the given key, holding a list of the
  82. // provided objects that can be marshaled by Zap.
  83. //
  84. // Note that these objects must implement zapcore.ObjectMarshaler directly.
  85. // That is, if you're trying to marshal a []Request, the MarshalLogObject
  86. // method must be declared on the Request type, not its pointer (*Request).
  87. // If it's on the pointer, use ObjectValues.
  88. //
  89. // Given an object that implements MarshalLogObject on the value receiver, you
  90. // can log a slice of those objects with Objects like so:
  91. //
  92. // type Author struct{ ... }
  93. // func (a Author) MarshalLogObject(enc zapcore.ObjectEncoder) error
  94. //
  95. // var authors []Author = ...
  96. // logger.Info("loading article", zap.Objects("authors", authors))
  97. //
  98. // Similarly, given a type that implements MarshalLogObject on its pointer
  99. // receiver, you can log a slice of pointers to that object with Objects like
  100. // so:
  101. //
  102. // type Request struct{ ... }
  103. // func (r *Request) MarshalLogObject(enc zapcore.ObjectEncoder) error
  104. //
  105. // var requests []*Request = ...
  106. // logger.Info("sending requests", zap.Objects("requests", requests))
  107. //
  108. // If instead, you have a slice of values of such an object, use the
  109. // ObjectValues constructor.
  110. //
  111. // var requests []Request = ...
  112. // logger.Info("sending requests", zap.ObjectValues("requests", requests))
  113. func Objects[T zapcore.ObjectMarshaler](key string, values []T) Field {
  114. return Array(key, objects[T](values))
  115. }
  116. type objects[T zapcore.ObjectMarshaler] []T
  117. func (os objects[T]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  118. for _, o := range os {
  119. if err := arr.AppendObject(o); err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }
  125. // ObjectMarshalerPtr is a constraint that specifies that the given type
  126. // implements zapcore.ObjectMarshaler on a pointer receiver.
  127. type ObjectMarshalerPtr[T any] interface {
  128. *T
  129. zapcore.ObjectMarshaler
  130. }
  131. // ObjectValues constructs a field with the given key, holding a list of the
  132. // provided objects, where pointers to these objects can be marshaled by Zap.
  133. //
  134. // Note that pointers to these objects must implement zapcore.ObjectMarshaler.
  135. // That is, if you're trying to marshal a []Request, the MarshalLogObject
  136. // method must be declared on the *Request type, not the value (Request).
  137. // If it's on the value, use Objects.
  138. //
  139. // Given an object that implements MarshalLogObject on the pointer receiver,
  140. // you can log a slice of those objects with ObjectValues like so:
  141. //
  142. // type Request struct{ ... }
  143. // func (r *Request) MarshalLogObject(enc zapcore.ObjectEncoder) error
  144. //
  145. // var requests []Request = ...
  146. // logger.Info("sending requests", zap.ObjectValues("requests", requests))
  147. //
  148. // If instead, you have a slice of pointers of such an object, use the Objects
  149. // field constructor.
  150. //
  151. // var requests []*Request = ...
  152. // logger.Info("sending requests", zap.Objects("requests", requests))
  153. func ObjectValues[T any, P ObjectMarshalerPtr[T]](key string, values []T) Field {
  154. return Array(key, objectValues[T, P](values))
  155. }
  156. type objectValues[T any, P ObjectMarshalerPtr[T]] []T
  157. func (os objectValues[T, P]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  158. for i := range os {
  159. // It is necessary for us to explicitly reference the "P" type.
  160. // We cannot simply pass "&os[i]" to AppendObject because its type
  161. // is "*T", which the type system does not consider as
  162. // implementing ObjectMarshaler.
  163. // Only the type "P" satisfies ObjectMarshaler, which we have
  164. // to convert "*T" to explicitly.
  165. var p P = &os[i]
  166. if err := arr.AppendObject(p); err != nil {
  167. return err
  168. }
  169. }
  170. return nil
  171. }
  172. // Strings constructs a field that carries a slice of strings.
  173. func Strings(key string, ss []string) Field {
  174. return Array(key, stringArray(ss))
  175. }
  176. // Stringers constructs a field with the given key, holding a list of the
  177. // output provided by the value's String method
  178. //
  179. // Given an object that implements String on the value receiver, you
  180. // can log a slice of those objects with Objects like so:
  181. //
  182. // type Request struct{ ... }
  183. // func (a Request) String() string
  184. //
  185. // var requests []Request = ...
  186. // logger.Info("sending requests", zap.Stringers("requests", requests))
  187. //
  188. // Note that these objects must implement fmt.Stringer directly.
  189. // That is, if you're trying to marshal a []Request, the String method
  190. // must be declared on the Request type, not its pointer (*Request).
  191. func Stringers[T fmt.Stringer](key string, values []T) Field {
  192. return Array(key, stringers[T](values))
  193. }
  194. type stringers[T fmt.Stringer] []T
  195. func (os stringers[T]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  196. for _, o := range os {
  197. arr.AppendString(o.String())
  198. }
  199. return nil
  200. }
  201. // Times constructs a field that carries a slice of time.Times.
  202. func Times(key string, ts []time.Time) Field {
  203. return Array(key, times(ts))
  204. }
  205. // Uints constructs a field that carries a slice of unsigned integers.
  206. func Uints(key string, nums []uint) Field {
  207. return Array(key, uints(nums))
  208. }
  209. // Uint64s constructs a field that carries a slice of unsigned integers.
  210. func Uint64s(key string, nums []uint64) Field {
  211. return Array(key, uint64s(nums))
  212. }
  213. // Uint32s constructs a field that carries a slice of unsigned integers.
  214. func Uint32s(key string, nums []uint32) Field {
  215. return Array(key, uint32s(nums))
  216. }
  217. // Uint16s constructs a field that carries a slice of unsigned integers.
  218. func Uint16s(key string, nums []uint16) Field {
  219. return Array(key, uint16s(nums))
  220. }
  221. // Uint8s constructs a field that carries a slice of unsigned integers.
  222. func Uint8s(key string, nums []uint8) Field {
  223. return Array(key, uint8s(nums))
  224. }
  225. // Uintptrs constructs a field that carries a slice of pointer addresses.
  226. func Uintptrs(key string, us []uintptr) Field {
  227. return Array(key, uintptrs(us))
  228. }
  229. // Errors constructs a field that carries a slice of errors.
  230. func Errors(key string, errs []error) Field {
  231. return Array(key, errArray(errs))
  232. }
  233. type bools []bool
  234. func (bs bools) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  235. for i := range bs {
  236. arr.AppendBool(bs[i])
  237. }
  238. return nil
  239. }
  240. type byteStringsArray [][]byte
  241. func (bss byteStringsArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  242. for i := range bss {
  243. arr.AppendByteString(bss[i])
  244. }
  245. return nil
  246. }
  247. type complex128s []complex128
  248. func (nums complex128s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  249. for i := range nums {
  250. arr.AppendComplex128(nums[i])
  251. }
  252. return nil
  253. }
  254. type complex64s []complex64
  255. func (nums complex64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  256. for i := range nums {
  257. arr.AppendComplex64(nums[i])
  258. }
  259. return nil
  260. }
  261. type durations []time.Duration
  262. func (ds durations) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  263. for i := range ds {
  264. arr.AppendDuration(ds[i])
  265. }
  266. return nil
  267. }
  268. type float64s []float64
  269. func (nums float64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  270. for i := range nums {
  271. arr.AppendFloat64(nums[i])
  272. }
  273. return nil
  274. }
  275. type float32s []float32
  276. func (nums float32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  277. for i := range nums {
  278. arr.AppendFloat32(nums[i])
  279. }
  280. return nil
  281. }
  282. type ints []int
  283. func (nums ints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  284. for i := range nums {
  285. arr.AppendInt(nums[i])
  286. }
  287. return nil
  288. }
  289. type int64s []int64
  290. func (nums int64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  291. for i := range nums {
  292. arr.AppendInt64(nums[i])
  293. }
  294. return nil
  295. }
  296. type int32s []int32
  297. func (nums int32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  298. for i := range nums {
  299. arr.AppendInt32(nums[i])
  300. }
  301. return nil
  302. }
  303. type int16s []int16
  304. func (nums int16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  305. for i := range nums {
  306. arr.AppendInt16(nums[i])
  307. }
  308. return nil
  309. }
  310. type int8s []int8
  311. func (nums int8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  312. for i := range nums {
  313. arr.AppendInt8(nums[i])
  314. }
  315. return nil
  316. }
  317. type stringArray []string
  318. func (ss stringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  319. for i := range ss {
  320. arr.AppendString(ss[i])
  321. }
  322. return nil
  323. }
  324. type times []time.Time
  325. func (ts times) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  326. for i := range ts {
  327. arr.AppendTime(ts[i])
  328. }
  329. return nil
  330. }
  331. type uints []uint
  332. func (nums uints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  333. for i := range nums {
  334. arr.AppendUint(nums[i])
  335. }
  336. return nil
  337. }
  338. type uint64s []uint64
  339. func (nums uint64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  340. for i := range nums {
  341. arr.AppendUint64(nums[i])
  342. }
  343. return nil
  344. }
  345. type uint32s []uint32
  346. func (nums uint32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  347. for i := range nums {
  348. arr.AppendUint32(nums[i])
  349. }
  350. return nil
  351. }
  352. type uint16s []uint16
  353. func (nums uint16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  354. for i := range nums {
  355. arr.AppendUint16(nums[i])
  356. }
  357. return nil
  358. }
  359. type uint8s []uint8
  360. func (nums uint8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  361. for i := range nums {
  362. arr.AppendUint8(nums[i])
  363. }
  364. return nil
  365. }
  366. type uintptrs []uintptr
  367. func (nums uintptrs) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  368. for i := range nums {
  369. arr.AppendUintptr(nums[i])
  370. }
  371. return nil
  372. }