field_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. "net"
  23. "sync"
  24. "testing"
  25. "time"
  26. "github.com/stretchr/testify/assert"
  27. "go.uber.org/zap/zapcore"
  28. )
  29. type username string
  30. func (n username) MarshalLogObject(enc zapcore.ObjectEncoder) error {
  31. enc.AddString("username", string(n))
  32. return nil
  33. }
  34. func assertCanBeReused(t testing.TB, field Field) {
  35. var wg sync.WaitGroup
  36. for i := 0; i < 100; i++ {
  37. enc := zapcore.NewMapObjectEncoder()
  38. // Ensure using the field in multiple encoders in separate goroutines
  39. // does not cause any races or panics.
  40. wg.Add(1)
  41. go func() {
  42. defer wg.Done()
  43. assert.NotPanics(t, func() {
  44. field.AddTo(enc)
  45. }, "Reusing a field should not cause issues")
  46. }()
  47. }
  48. wg.Wait()
  49. }
  50. func TestFieldConstructors(t *testing.T) {
  51. // Interface types.
  52. addr := net.ParseIP("1.2.3.4")
  53. name := username("phil")
  54. ints := []int{5, 6}
  55. // Helpful values for use in constructing pointers to primitives below.
  56. var (
  57. boolVal bool = true
  58. complex128Val complex128 = complex(0, 0)
  59. complex64Val complex64 = complex(0, 0)
  60. durationVal time.Duration = time.Second
  61. float64Val float64 = 1.0
  62. float32Val float32 = 1.0
  63. intVal int = 1
  64. int64Val int64 = 1
  65. int32Val int32 = 1
  66. int16Val int16 = 1
  67. int8Val int8 = 1
  68. stringVal string = "hello"
  69. timeVal time.Time = time.Unix(100000, 0)
  70. uintVal uint = 1
  71. uint64Val uint64 = 1
  72. uint32Val uint32 = 1
  73. uint16Val uint16 = 1
  74. uint8Val uint8 = 1
  75. uintptrVal uintptr = 1
  76. )
  77. tests := []struct {
  78. name string
  79. field Field
  80. expect Field
  81. }{
  82. {"Skip", Field{Type: zapcore.SkipType}, Skip()},
  83. {"Binary", Field{Key: "k", Type: zapcore.BinaryType, Interface: []byte("ab12")}, Binary("k", []byte("ab12"))},
  84. {"Bool", Field{Key: "k", Type: zapcore.BoolType, Integer: 1}, Bool("k", true)},
  85. {"Bool", Field{Key: "k", Type: zapcore.BoolType, Integer: 1}, Bool("k", true)},
  86. {"ByteString", Field{Key: "k", Type: zapcore.ByteStringType, Interface: []byte("ab12")}, ByteString("k", []byte("ab12"))},
  87. {"Complex128", Field{Key: "k", Type: zapcore.Complex128Type, Interface: 1 + 2i}, Complex128("k", 1+2i)},
  88. {"Complex64", Field{Key: "k", Type: zapcore.Complex64Type, Interface: complex64(1 + 2i)}, Complex64("k", 1+2i)},
  89. {"Duration", Field{Key: "k", Type: zapcore.DurationType, Integer: 1}, Duration("k", 1)},
  90. {"Int", Field{Key: "k", Type: zapcore.Int64Type, Integer: 1}, Int("k", 1)},
  91. {"Int64", Field{Key: "k", Type: zapcore.Int64Type, Integer: 1}, Int64("k", 1)},
  92. {"Int32", Field{Key: "k", Type: zapcore.Int32Type, Integer: 1}, Int32("k", 1)},
  93. {"Int16", Field{Key: "k", Type: zapcore.Int16Type, Integer: 1}, Int16("k", 1)},
  94. {"Int8", Field{Key: "k", Type: zapcore.Int8Type, Integer: 1}, Int8("k", 1)},
  95. {"String", Field{Key: "k", Type: zapcore.StringType, String: "foo"}, String("k", "foo")},
  96. {"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: 0, Interface: time.UTC}, Time("k", time.Unix(0, 0).In(time.UTC))},
  97. {"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: 1000, Interface: time.UTC}, Time("k", time.Unix(0, 1000).In(time.UTC))},
  98. {"Uint", Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint("k", 1)},
  99. {"Uint64", Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint64("k", 1)},
  100. {"Uint32", Field{Key: "k", Type: zapcore.Uint32Type, Integer: 1}, Uint32("k", 1)},
  101. {"Uint16", Field{Key: "k", Type: zapcore.Uint16Type, Integer: 1}, Uint16("k", 1)},
  102. {"Uint8", Field{Key: "k", Type: zapcore.Uint8Type, Integer: 1}, Uint8("k", 1)},
  103. {"Uintptr", Field{Key: "k", Type: zapcore.UintptrType, Integer: 10}, Uintptr("k", 0xa)},
  104. {"Reflect", Field{Key: "k", Type: zapcore.ReflectType, Interface: ints}, Reflect("k", ints)},
  105. {"Reflect", Field{Key: "k", Type: zapcore.ReflectType}, Reflect("k", nil)},
  106. {"Stringer", Field{Key: "k", Type: zapcore.StringerType, Interface: addr}, Stringer("k", addr)},
  107. {"Object", Field{Key: "k", Type: zapcore.ObjectMarshalerType, Interface: name}, Object("k", name)},
  108. {"Any:ObjectMarshaler", Any("k", name), Object("k", name)},
  109. {"Any:ArrayMarshaler", Any("k", bools([]bool{true})), Array("k", bools([]bool{true}))},
  110. {"Any:Stringer", Any("k", addr), Stringer("k", addr)},
  111. {"Any:Bool", Any("k", true), Bool("k", true)},
  112. {"Any:Bools", Any("k", []bool{true}), Bools("k", []bool{true})},
  113. {"Any:Byte", Any("k", byte(1)), Uint8("k", 1)},
  114. {"Any:Bytes", Any("k", []byte{1}), Binary("k", []byte{1})},
  115. {"Any:Complex128", Any("k", 1+2i), Complex128("k", 1+2i)},
  116. {"Any:Complex128s", Any("k", []complex128{1 + 2i}), Complex128s("k", []complex128{1 + 2i})},
  117. {"Any:Complex64", Any("k", complex64(1+2i)), Complex64("k", 1+2i)},
  118. {"Any:Complex64s", Any("k", []complex64{1 + 2i}), Complex64s("k", []complex64{1 + 2i})},
  119. {"Any:Float64", Any("k", 3.14), Float64("k", 3.14)},
  120. {"Any:Float64s", Any("k", []float64{3.14}), Float64s("k", []float64{3.14})},
  121. {"Any:Float32", Any("k", float32(3.14)), Float32("k", 3.14)},
  122. {"Any:Float32s", Any("k", []float32{3.14}), Float32s("k", []float32{3.14})},
  123. {"Any:Int", Any("k", 1), Int("k", 1)},
  124. {"Any:Ints", Any("k", []int{1}), Ints("k", []int{1})},
  125. {"Any:Int64", Any("k", int64(1)), Int64("k", 1)},
  126. {"Any:Int64s", Any("k", []int64{1}), Int64s("k", []int64{1})},
  127. {"Any:Int32", Any("k", int32(1)), Int32("k", 1)},
  128. {"Any:Int32s", Any("k", []int32{1}), Int32s("k", []int32{1})},
  129. {"Any:Int16", Any("k", int16(1)), Int16("k", 1)},
  130. {"Any:Int16s", Any("k", []int16{1}), Int16s("k", []int16{1})},
  131. {"Any:Int8", Any("k", int8(1)), Int8("k", 1)},
  132. {"Any:Int8s", Any("k", []int8{1}), Int8s("k", []int8{1})},
  133. {"Any:Rune", Any("k", rune(1)), Int32("k", 1)},
  134. {"Any:Runes", Any("k", []rune{1}), Int32s("k", []int32{1})},
  135. {"Any:String", Any("k", "v"), String("k", "v")},
  136. {"Any:Strings", Any("k", []string{"v"}), Strings("k", []string{"v"})},
  137. {"Any:Uint", Any("k", uint(1)), Uint("k", 1)},
  138. {"Any:Uints", Any("k", []uint{1}), Uints("k", []uint{1})},
  139. {"Any:Uint64", Any("k", uint64(1)), Uint64("k", 1)},
  140. {"Any:Uint64s", Any("k", []uint64{1}), Uint64s("k", []uint64{1})},
  141. {"Any:Uint32", Any("k", uint32(1)), Uint32("k", 1)},
  142. {"Any:Uint32s", Any("k", []uint32{1}), Uint32s("k", []uint32{1})},
  143. {"Any:Uint16", Any("k", uint16(1)), Uint16("k", 1)},
  144. {"Any:Uint16s", Any("k", []uint16{1}), Uint16s("k", []uint16{1})},
  145. {"Any:Uint8", Any("k", uint8(1)), Uint8("k", 1)},
  146. {"Any:Uint8s", Any("k", []uint8{1}), Binary("k", []uint8{1})},
  147. {"Any:Uintptr", Any("k", uintptr(1)), Uintptr("k", 1)},
  148. {"Any:Uintptrs", Any("k", []uintptr{1}), Uintptrs("k", []uintptr{1})},
  149. {"Any:Time", Any("k", time.Unix(0, 0)), Time("k", time.Unix(0, 0))},
  150. {"Any:Times", Any("k", []time.Time{time.Unix(0, 0)}), Times("k", []time.Time{time.Unix(0, 0)})},
  151. {"Any:Duration", Any("k", time.Second), Duration("k", time.Second)},
  152. {"Any:Durations", Any("k", []time.Duration{time.Second}), Durations("k", []time.Duration{time.Second})},
  153. {"Any:Fallback", Any("k", struct{}{}), Reflect("k", struct{}{})},
  154. {"Ptr:Bool", Boolp("k", nil), nilField("k")},
  155. {"Ptr:Bool", Boolp("k", &boolVal), Bool("k", boolVal)},
  156. {"Any:PtrBool", Any("k", (*bool)(nil)), nilField("k")},
  157. {"Any:PtrBool", Any("k", &boolVal), Bool("k", boolVal)},
  158. {"Ptr:Complex128", Complex128p("k", nil), nilField("k")},
  159. {"Ptr:Complex128", Complex128p("k", &complex128Val), Complex128("k", complex128Val)},
  160. {"Any:PtrComplex128", Any("k", (*complex128)(nil)), nilField("k")},
  161. {"Any:PtrComplex128", Any("k", &complex128Val), Complex128("k", complex128Val)},
  162. {"Ptr:Complex64", Complex64p("k", nil), nilField("k")},
  163. {"Ptr:Complex64", Complex64p("k", &complex64Val), Complex64("k", complex64Val)},
  164. {"Any:PtrComplex64", Any("k", (*complex64)(nil)), nilField("k")},
  165. {"Any:PtrComplex64", Any("k", &complex64Val), Complex64("k", complex64Val)},
  166. {"Ptr:Duration", Durationp("k", nil), nilField("k")},
  167. {"Ptr:Duration", Durationp("k", &durationVal), Duration("k", durationVal)},
  168. {"Any:PtrDuration", Any("k", (*time.Duration)(nil)), nilField("k")},
  169. {"Any:PtrDuration", Any("k", &durationVal), Duration("k", durationVal)},
  170. {"Ptr:Float64", Float64p("k", nil), nilField("k")},
  171. {"Ptr:Float64", Float64p("k", &float64Val), Float64("k", float64Val)},
  172. {"Any:PtrFloat64", Any("k", (*float64)(nil)), nilField("k")},
  173. {"Any:PtrFloat64", Any("k", &float64Val), Float64("k", float64Val)},
  174. {"Ptr:Float32", Float32p("k", nil), nilField("k")},
  175. {"Ptr:Float32", Float32p("k", &float32Val), Float32("k", float32Val)},
  176. {"Any:PtrFloat32", Any("k", (*float32)(nil)), nilField("k")},
  177. {"Any:PtrFloat32", Any("k", &float32Val), Float32("k", float32Val)},
  178. {"Ptr:Int", Intp("k", nil), nilField("k")},
  179. {"Ptr:Int", Intp("k", &intVal), Int("k", intVal)},
  180. {"Any:PtrInt", Any("k", (*int)(nil)), nilField("k")},
  181. {"Any:PtrInt", Any("k", &intVal), Int("k", intVal)},
  182. {"Ptr:Int64", Int64p("k", nil), nilField("k")},
  183. {"Ptr:Int64", Int64p("k", &int64Val), Int64("k", int64Val)},
  184. {"Any:PtrInt64", Any("k", (*int64)(nil)), nilField("k")},
  185. {"Any:PtrInt64", Any("k", &int64Val), Int64("k", int64Val)},
  186. {"Ptr:Int32", Int32p("k", nil), nilField("k")},
  187. {"Ptr:Int32", Int32p("k", &int32Val), Int32("k", int32Val)},
  188. {"Any:PtrInt32", Any("k", (*int32)(nil)), nilField("k")},
  189. {"Any:PtrInt32", Any("k", &int32Val), Int32("k", int32Val)},
  190. {"Ptr:Int16", Int16p("k", nil), nilField("k")},
  191. {"Ptr:Int16", Int16p("k", &int16Val), Int16("k", int16Val)},
  192. {"Any:PtrInt16", Any("k", (*int16)(nil)), nilField("k")},
  193. {"Any:PtrInt16", Any("k", &int16Val), Int16("k", int16Val)},
  194. {"Ptr:Int8", Int8p("k", nil), nilField("k")},
  195. {"Ptr:Int8", Int8p("k", &int8Val), Int8("k", int8Val)},
  196. {"Any:PtrInt8", Any("k", (*int8)(nil)), nilField("k")},
  197. {"Any:PtrInt8", Any("k", &int8Val), Int8("k", int8Val)},
  198. {"Ptr:String", Stringp("k", nil), nilField("k")},
  199. {"Ptr:String", Stringp("k", &stringVal), String("k", stringVal)},
  200. {"Any:PtrString", Any("k", (*string)(nil)), nilField("k")},
  201. {"Any:PtrString", Any("k", &stringVal), String("k", stringVal)},
  202. {"Ptr:Time", Timep("k", nil), nilField("k")},
  203. {"Ptr:Time", Timep("k", &timeVal), Time("k", timeVal)},
  204. {"Any:PtrTime", Any("k", (*time.Time)(nil)), nilField("k")},
  205. {"Any:PtrTime", Any("k", &timeVal), Time("k", timeVal)},
  206. {"Ptr:Uint", Uintp("k", nil), nilField("k")},
  207. {"Ptr:Uint", Uintp("k", &uintVal), Uint("k", uintVal)},
  208. {"Any:PtrUint", Any("k", (*uint)(nil)), nilField("k")},
  209. {"Any:PtrUint", Any("k", &uintVal), Uint("k", uintVal)},
  210. {"Ptr:Uint64", Uint64p("k", nil), nilField("k")},
  211. {"Ptr:Uint64", Uint64p("k", &uint64Val), Uint64("k", uint64Val)},
  212. {"Any:PtrUint64", Any("k", (*uint64)(nil)), nilField("k")},
  213. {"Any:PtrUint64", Any("k", &uint64Val), Uint64("k", uint64Val)},
  214. {"Ptr:Uint32", Uint32p("k", nil), nilField("k")},
  215. {"Ptr:Uint32", Uint32p("k", &uint32Val), Uint32("k", uint32Val)},
  216. {"Any:PtrUint32", Any("k", (*uint32)(nil)), nilField("k")},
  217. {"Any:PtrUint32", Any("k", &uint32Val), Uint32("k", uint32Val)},
  218. {"Ptr:Uint16", Uint16p("k", nil), nilField("k")},
  219. {"Ptr:Uint16", Uint16p("k", &uint16Val), Uint16("k", uint16Val)},
  220. {"Any:PtrUint16", Any("k", (*uint16)(nil)), nilField("k")},
  221. {"Any:PtrUint16", Any("k", &uint16Val), Uint16("k", uint16Val)},
  222. {"Ptr:Uint8", Uint8p("k", nil), nilField("k")},
  223. {"Ptr:Uint8", Uint8p("k", &uint8Val), Uint8("k", uint8Val)},
  224. {"Any:PtrUint8", Any("k", (*uint8)(nil)), nilField("k")},
  225. {"Any:PtrUint8", Any("k", &uint8Val), Uint8("k", uint8Val)},
  226. {"Ptr:Uintptr", Uintptrp("k", nil), nilField("k")},
  227. {"Ptr:Uintptr", Uintptrp("k", &uintptrVal), Uintptr("k", uintptrVal)},
  228. {"Any:PtrUintptr", Any("k", (*uintptr)(nil)), nilField("k")},
  229. {"Any:PtrUintptr", Any("k", &uintptrVal), Uintptr("k", uintptrVal)},
  230. {"Namespace", Namespace("k"), Field{Key: "k", Type: zapcore.NamespaceType}},
  231. }
  232. for _, tt := range tests {
  233. if !assert.Equal(t, tt.expect, tt.field, "Unexpected output from convenience field constructor %s.", tt.name) {
  234. t.Logf("type expected: %T\nGot: %T", tt.expect.Interface, tt.field.Interface)
  235. }
  236. assertCanBeReused(t, tt.field)
  237. }
  238. }
  239. func TestStackField(t *testing.T) {
  240. f := Stack("stacktrace")
  241. assert.Equal(t, "stacktrace", f.Key, "Unexpected field key.")
  242. assert.Equal(t, zapcore.StringType, f.Type, "Unexpected field type.")
  243. assert.Equal(t, takeStacktrace(), f.String, "Unexpected stack trace")
  244. assertCanBeReused(t, f)
  245. }