array.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. "time"
  23. "go.uber.org/zap/zapcore"
  24. )
  25. // Array constructs a field with the given key and ArrayMarshaler. It provides
  26. // a flexible, but still type-safe and efficient, way to add array-like types
  27. // to the logging context. The struct's MarshalLogArray method is called lazily.
  28. func Array(key string, val zapcore.ArrayMarshaler) Field {
  29. return Field{Key: key, Type: zapcore.ArrayMarshalerType, Interface: val}
  30. }
  31. // Bools constructs a field that carries a slice of bools.
  32. func Bools(key string, bs []bool) Field {
  33. return Array(key, bools(bs))
  34. }
  35. // ByteStrings constructs a field that carries a slice of []byte, each of which
  36. // must be UTF-8 encoded text.
  37. func ByteStrings(key string, bss [][]byte) Field {
  38. return Array(key, byteStringsArray(bss))
  39. }
  40. // Complex128s constructs a field that carries a slice of complex numbers.
  41. func Complex128s(key string, nums []complex128) Field {
  42. return Array(key, complex128s(nums))
  43. }
  44. // Complex64s constructs a field that carries a slice of complex numbers.
  45. func Complex64s(key string, nums []complex64) Field {
  46. return Array(key, complex64s(nums))
  47. }
  48. // Durations constructs a field that carries a slice of time.Durations.
  49. func Durations(key string, ds []time.Duration) Field {
  50. return Array(key, durations(ds))
  51. }
  52. // Float64s constructs a field that carries a slice of floats.
  53. func Float64s(key string, nums []float64) Field {
  54. return Array(key, float64s(nums))
  55. }
  56. // Float32s constructs a field that carries a slice of floats.
  57. func Float32s(key string, nums []float32) Field {
  58. return Array(key, float32s(nums))
  59. }
  60. // Ints constructs a field that carries a slice of integers.
  61. func Ints(key string, nums []int) Field {
  62. return Array(key, ints(nums))
  63. }
  64. // Int64s constructs a field that carries a slice of integers.
  65. func Int64s(key string, nums []int64) Field {
  66. return Array(key, int64s(nums))
  67. }
  68. // Int32s constructs a field that carries a slice of integers.
  69. func Int32s(key string, nums []int32) Field {
  70. return Array(key, int32s(nums))
  71. }
  72. // Int16s constructs a field that carries a slice of integers.
  73. func Int16s(key string, nums []int16) Field {
  74. return Array(key, int16s(nums))
  75. }
  76. // Int8s constructs a field that carries a slice of integers.
  77. func Int8s(key string, nums []int8) Field {
  78. return Array(key, int8s(nums))
  79. }
  80. // Strings constructs a field that carries a slice of strings.
  81. func Strings(key string, ss []string) Field {
  82. return Array(key, stringArray(ss))
  83. }
  84. // Times constructs a field that carries a slice of time.Times.
  85. func Times(key string, ts []time.Time) Field {
  86. return Array(key, times(ts))
  87. }
  88. // Uints constructs a field that carries a slice of unsigned integers.
  89. func Uints(key string, nums []uint) Field {
  90. return Array(key, uints(nums))
  91. }
  92. // Uint64s constructs a field that carries a slice of unsigned integers.
  93. func Uint64s(key string, nums []uint64) Field {
  94. return Array(key, uint64s(nums))
  95. }
  96. // Uint32s constructs a field that carries a slice of unsigned integers.
  97. func Uint32s(key string, nums []uint32) Field {
  98. return Array(key, uint32s(nums))
  99. }
  100. // Uint16s constructs a field that carries a slice of unsigned integers.
  101. func Uint16s(key string, nums []uint16) Field {
  102. return Array(key, uint16s(nums))
  103. }
  104. // Uint8s constructs a field that carries a slice of unsigned integers.
  105. func Uint8s(key string, nums []uint8) Field {
  106. return Array(key, uint8s(nums))
  107. }
  108. // Uintptrs constructs a field that carries a slice of pointer addresses.
  109. func Uintptrs(key string, us []uintptr) Field {
  110. return Array(key, uintptrs(us))
  111. }
  112. // Errors constructs a field that carries a slice of errors.
  113. func Errors(key string, errs []error) Field {
  114. return Array(key, errArray(errs))
  115. }
  116. type bools []bool
  117. func (bs bools) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  118. for i := range bs {
  119. arr.AppendBool(bs[i])
  120. }
  121. return nil
  122. }
  123. type byteStringsArray [][]byte
  124. func (bss byteStringsArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  125. for i := range bss {
  126. arr.AppendByteString(bss[i])
  127. }
  128. return nil
  129. }
  130. type complex128s []complex128
  131. func (nums complex128s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  132. for i := range nums {
  133. arr.AppendComplex128(nums[i])
  134. }
  135. return nil
  136. }
  137. type complex64s []complex64
  138. func (nums complex64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  139. for i := range nums {
  140. arr.AppendComplex64(nums[i])
  141. }
  142. return nil
  143. }
  144. type durations []time.Duration
  145. func (ds durations) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  146. for i := range ds {
  147. arr.AppendDuration(ds[i])
  148. }
  149. return nil
  150. }
  151. type float64s []float64
  152. func (nums float64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  153. for i := range nums {
  154. arr.AppendFloat64(nums[i])
  155. }
  156. return nil
  157. }
  158. type float32s []float32
  159. func (nums float32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  160. for i := range nums {
  161. arr.AppendFloat32(nums[i])
  162. }
  163. return nil
  164. }
  165. type ints []int
  166. func (nums ints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  167. for i := range nums {
  168. arr.AppendInt(nums[i])
  169. }
  170. return nil
  171. }
  172. type int64s []int64
  173. func (nums int64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  174. for i := range nums {
  175. arr.AppendInt64(nums[i])
  176. }
  177. return nil
  178. }
  179. type int32s []int32
  180. func (nums int32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  181. for i := range nums {
  182. arr.AppendInt32(nums[i])
  183. }
  184. return nil
  185. }
  186. type int16s []int16
  187. func (nums int16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  188. for i := range nums {
  189. arr.AppendInt16(nums[i])
  190. }
  191. return nil
  192. }
  193. type int8s []int8
  194. func (nums int8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  195. for i := range nums {
  196. arr.AppendInt8(nums[i])
  197. }
  198. return nil
  199. }
  200. type stringArray []string
  201. func (ss stringArray) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  202. for i := range ss {
  203. arr.AppendString(ss[i])
  204. }
  205. return nil
  206. }
  207. type times []time.Time
  208. func (ts times) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  209. for i := range ts {
  210. arr.AppendTime(ts[i])
  211. }
  212. return nil
  213. }
  214. type uints []uint
  215. func (nums uints) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  216. for i := range nums {
  217. arr.AppendUint(nums[i])
  218. }
  219. return nil
  220. }
  221. type uint64s []uint64
  222. func (nums uint64s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  223. for i := range nums {
  224. arr.AppendUint64(nums[i])
  225. }
  226. return nil
  227. }
  228. type uint32s []uint32
  229. func (nums uint32s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  230. for i := range nums {
  231. arr.AppendUint32(nums[i])
  232. }
  233. return nil
  234. }
  235. type uint16s []uint16
  236. func (nums uint16s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  237. for i := range nums {
  238. arr.AppendUint16(nums[i])
  239. }
  240. return nil
  241. }
  242. type uint8s []uint8
  243. func (nums uint8s) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  244. for i := range nums {
  245. arr.AppendUint8(nums[i])
  246. }
  247. return nil
  248. }
  249. type uintptrs []uintptr
  250. func (nums uintptrs) MarshalLogArray(arr zapcore.ArrayEncoder) error {
  251. for i := range nums {
  252. arr.AppendUintptr(nums[i])
  253. }
  254. return nil
  255. }