json_encoder.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 zapcore
  21. import (
  22. "encoding/base64"
  23. "encoding/json"
  24. "math"
  25. "sync"
  26. "time"
  27. "unicode/utf8"
  28. "go.uber.org/zap/buffer"
  29. "go.uber.org/zap/internal/bufferpool"
  30. )
  31. // For JSON-escaping; see jsonEncoder.safeAddString below.
  32. const _hex = "0123456789abcdef"
  33. var _jsonPool = sync.Pool{New: func() interface{} {
  34. return &jsonEncoder{}
  35. }}
  36. func getJSONEncoder() *jsonEncoder {
  37. return _jsonPool.Get().(*jsonEncoder)
  38. }
  39. func putJSONEncoder(enc *jsonEncoder) {
  40. if enc.reflectBuf != nil {
  41. enc.reflectBuf.Free()
  42. }
  43. enc.EncoderConfig = nil
  44. enc.buf = nil
  45. enc.spaced = false
  46. enc.openNamespaces = 0
  47. enc.reflectBuf = nil
  48. enc.reflectEnc = nil
  49. _jsonPool.Put(enc)
  50. }
  51. type jsonEncoder struct {
  52. *EncoderConfig
  53. buf *buffer.Buffer
  54. spaced bool // include spaces after colons and commas
  55. openNamespaces int
  56. // for encoding generic values by reflection
  57. reflectBuf *buffer.Buffer
  58. reflectEnc *json.Encoder
  59. }
  60. // NewJSONEncoder creates a fast, low-allocation JSON encoder. The encoder
  61. // appropriately escapes all field keys and values.
  62. //
  63. // Note that the encoder doesn't deduplicate keys, so it's possible to produce
  64. // a message like
  65. // {"foo":"bar","foo":"baz"}
  66. // This is permitted by the JSON specification, but not encouraged. Many
  67. // libraries will ignore duplicate key-value pairs (typically keeping the last
  68. // pair) when unmarshaling, but users should attempt to avoid adding duplicate
  69. // keys.
  70. func NewJSONEncoder(cfg EncoderConfig) Encoder {
  71. return newJSONEncoder(cfg, false)
  72. }
  73. func newJSONEncoder(cfg EncoderConfig, spaced bool) *jsonEncoder {
  74. return &jsonEncoder{
  75. EncoderConfig: &cfg,
  76. buf: bufferpool.Get(),
  77. spaced: spaced,
  78. }
  79. }
  80. func (enc *jsonEncoder) AddArray(key string, arr ArrayMarshaler) error {
  81. enc.addKey(key)
  82. return enc.AppendArray(arr)
  83. }
  84. func (enc *jsonEncoder) AddObject(key string, obj ObjectMarshaler) error {
  85. enc.addKey(key)
  86. return enc.AppendObject(obj)
  87. }
  88. func (enc *jsonEncoder) AddBinary(key string, val []byte) {
  89. enc.AddString(key, base64.StdEncoding.EncodeToString(val))
  90. }
  91. func (enc *jsonEncoder) AddByteString(key string, val []byte) {
  92. enc.addKey(key)
  93. enc.AppendByteString(val)
  94. }
  95. func (enc *jsonEncoder) AddBool(key string, val bool) {
  96. enc.addKey(key)
  97. enc.AppendBool(val)
  98. }
  99. func (enc *jsonEncoder) AddComplex128(key string, val complex128) {
  100. enc.addKey(key)
  101. enc.AppendComplex128(val)
  102. }
  103. func (enc *jsonEncoder) AddDuration(key string, val time.Duration) {
  104. enc.addKey(key)
  105. enc.AppendDuration(val)
  106. }
  107. func (enc *jsonEncoder) AddFloat64(key string, val float64) {
  108. enc.addKey(key)
  109. enc.AppendFloat64(val)
  110. }
  111. func (enc *jsonEncoder) AddInt64(key string, val int64) {
  112. enc.addKey(key)
  113. enc.AppendInt64(val)
  114. }
  115. func (enc *jsonEncoder) resetReflectBuf() {
  116. if enc.reflectBuf == nil {
  117. enc.reflectBuf = bufferpool.Get()
  118. enc.reflectEnc = json.NewEncoder(enc.reflectBuf)
  119. // For consistency with our custom JSON encoder.
  120. enc.reflectEnc.SetEscapeHTML(false)
  121. } else {
  122. enc.reflectBuf.Reset()
  123. }
  124. }
  125. var nullLiteralBytes = []byte("null")
  126. // Only invoke the standard JSON encoder if there is actually something to
  127. // encode; otherwise write JSON null literal directly.
  128. func (enc *jsonEncoder) encodeReflected(obj interface{}) ([]byte, error) {
  129. if obj == nil {
  130. return nullLiteralBytes, nil
  131. }
  132. enc.resetReflectBuf()
  133. if err := enc.reflectEnc.Encode(obj); err != nil {
  134. return nil, err
  135. }
  136. enc.reflectBuf.TrimNewline()
  137. return enc.reflectBuf.Bytes(), nil
  138. }
  139. func (enc *jsonEncoder) AddReflected(key string, obj interface{}) error {
  140. valueBytes, err := enc.encodeReflected(obj)
  141. if err != nil {
  142. return err
  143. }
  144. enc.addKey(key)
  145. _, err = enc.buf.Write(valueBytes)
  146. return err
  147. }
  148. func (enc *jsonEncoder) OpenNamespace(key string) {
  149. enc.addKey(key)
  150. enc.buf.AppendByte('{')
  151. enc.openNamespaces++
  152. }
  153. func (enc *jsonEncoder) AddString(key, val string) {
  154. enc.addKey(key)
  155. enc.AppendString(val)
  156. }
  157. func (enc *jsonEncoder) AddTime(key string, val time.Time) {
  158. enc.addKey(key)
  159. enc.AppendTime(val)
  160. }
  161. func (enc *jsonEncoder) AddUint64(key string, val uint64) {
  162. enc.addKey(key)
  163. enc.AppendUint64(val)
  164. }
  165. func (enc *jsonEncoder) AppendArray(arr ArrayMarshaler) error {
  166. enc.addElementSeparator()
  167. enc.buf.AppendByte('[')
  168. err := arr.MarshalLogArray(enc)
  169. enc.buf.AppendByte(']')
  170. return err
  171. }
  172. func (enc *jsonEncoder) AppendObject(obj ObjectMarshaler) error {
  173. enc.addElementSeparator()
  174. enc.buf.AppendByte('{')
  175. err := obj.MarshalLogObject(enc)
  176. enc.buf.AppendByte('}')
  177. return err
  178. }
  179. func (enc *jsonEncoder) AppendBool(val bool) {
  180. enc.addElementSeparator()
  181. enc.buf.AppendBool(val)
  182. }
  183. func (enc *jsonEncoder) AppendByteString(val []byte) {
  184. enc.addElementSeparator()
  185. enc.buf.AppendByte('"')
  186. enc.safeAddByteString(val)
  187. enc.buf.AppendByte('"')
  188. }
  189. func (enc *jsonEncoder) AppendComplex128(val complex128) {
  190. enc.addElementSeparator()
  191. // Cast to a platform-independent, fixed-size type.
  192. r, i := float64(real(val)), float64(imag(val))
  193. enc.buf.AppendByte('"')
  194. // Because we're always in a quoted string, we can use strconv without
  195. // special-casing NaN and +/-Inf.
  196. enc.buf.AppendFloat(r, 64)
  197. enc.buf.AppendByte('+')
  198. enc.buf.AppendFloat(i, 64)
  199. enc.buf.AppendByte('i')
  200. enc.buf.AppendByte('"')
  201. }
  202. func (enc *jsonEncoder) AppendDuration(val time.Duration) {
  203. cur := enc.buf.Len()
  204. enc.EncodeDuration(val, enc)
  205. if cur == enc.buf.Len() {
  206. // User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
  207. // JSON valid.
  208. enc.AppendInt64(int64(val))
  209. }
  210. }
  211. func (enc *jsonEncoder) AppendInt64(val int64) {
  212. enc.addElementSeparator()
  213. enc.buf.AppendInt(val)
  214. }
  215. func (enc *jsonEncoder) AppendReflected(val interface{}) error {
  216. valueBytes, err := enc.encodeReflected(val)
  217. if err != nil {
  218. return err
  219. }
  220. enc.addElementSeparator()
  221. _, err = enc.buf.Write(valueBytes)
  222. return err
  223. }
  224. func (enc *jsonEncoder) AppendString(val string) {
  225. enc.addElementSeparator()
  226. enc.buf.AppendByte('"')
  227. enc.safeAddString(val)
  228. enc.buf.AppendByte('"')
  229. }
  230. func (enc *jsonEncoder) AppendTimeLayout(time time.Time, layout string) {
  231. enc.addElementSeparator()
  232. enc.buf.AppendByte('"')
  233. enc.buf.AppendTime(time, layout)
  234. enc.buf.AppendByte('"')
  235. }
  236. func (enc *jsonEncoder) AppendTime(val time.Time) {
  237. cur := enc.buf.Len()
  238. enc.EncodeTime(val, enc)
  239. if cur == enc.buf.Len() {
  240. // User-supplied EncodeTime is a no-op. Fall back to nanos since epoch to keep
  241. // output JSON valid.
  242. enc.AppendInt64(val.UnixNano())
  243. }
  244. }
  245. func (enc *jsonEncoder) AppendUint64(val uint64) {
  246. enc.addElementSeparator()
  247. enc.buf.AppendUint(val)
  248. }
  249. func (enc *jsonEncoder) AddComplex64(k string, v complex64) { enc.AddComplex128(k, complex128(v)) }
  250. func (enc *jsonEncoder) AddFloat32(k string, v float32) { enc.AddFloat64(k, float64(v)) }
  251. func (enc *jsonEncoder) AddInt(k string, v int) { enc.AddInt64(k, int64(v)) }
  252. func (enc *jsonEncoder) AddInt32(k string, v int32) { enc.AddInt64(k, int64(v)) }
  253. func (enc *jsonEncoder) AddInt16(k string, v int16) { enc.AddInt64(k, int64(v)) }
  254. func (enc *jsonEncoder) AddInt8(k string, v int8) { enc.AddInt64(k, int64(v)) }
  255. func (enc *jsonEncoder) AddUint(k string, v uint) { enc.AddUint64(k, uint64(v)) }
  256. func (enc *jsonEncoder) AddUint32(k string, v uint32) { enc.AddUint64(k, uint64(v)) }
  257. func (enc *jsonEncoder) AddUint16(k string, v uint16) { enc.AddUint64(k, uint64(v)) }
  258. func (enc *jsonEncoder) AddUint8(k string, v uint8) { enc.AddUint64(k, uint64(v)) }
  259. func (enc *jsonEncoder) AddUintptr(k string, v uintptr) { enc.AddUint64(k, uint64(v)) }
  260. func (enc *jsonEncoder) AppendComplex64(v complex64) { enc.AppendComplex128(complex128(v)) }
  261. func (enc *jsonEncoder) AppendFloat64(v float64) { enc.appendFloat(v, 64) }
  262. func (enc *jsonEncoder) AppendFloat32(v float32) { enc.appendFloat(float64(v), 32) }
  263. func (enc *jsonEncoder) AppendInt(v int) { enc.AppendInt64(int64(v)) }
  264. func (enc *jsonEncoder) AppendInt32(v int32) { enc.AppendInt64(int64(v)) }
  265. func (enc *jsonEncoder) AppendInt16(v int16) { enc.AppendInt64(int64(v)) }
  266. func (enc *jsonEncoder) AppendInt8(v int8) { enc.AppendInt64(int64(v)) }
  267. func (enc *jsonEncoder) AppendUint(v uint) { enc.AppendUint64(uint64(v)) }
  268. func (enc *jsonEncoder) AppendUint32(v uint32) { enc.AppendUint64(uint64(v)) }
  269. func (enc *jsonEncoder) AppendUint16(v uint16) { enc.AppendUint64(uint64(v)) }
  270. func (enc *jsonEncoder) AppendUint8(v uint8) { enc.AppendUint64(uint64(v)) }
  271. func (enc *jsonEncoder) AppendUintptr(v uintptr) { enc.AppendUint64(uint64(v)) }
  272. func (enc *jsonEncoder) Clone() Encoder {
  273. clone := enc.clone()
  274. clone.buf.Write(enc.buf.Bytes())
  275. return clone
  276. }
  277. func (enc *jsonEncoder) clone() *jsonEncoder {
  278. clone := getJSONEncoder()
  279. clone.EncoderConfig = enc.EncoderConfig
  280. clone.spaced = enc.spaced
  281. clone.openNamespaces = enc.openNamespaces
  282. clone.buf = bufferpool.Get()
  283. return clone
  284. }
  285. func (enc *jsonEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, error) {
  286. final := enc.clone()
  287. final.buf.AppendByte('{')
  288. if final.LevelKey != "" {
  289. final.addKey(final.LevelKey)
  290. cur := final.buf.Len()
  291. final.EncodeLevel(ent.Level, final)
  292. if cur == final.buf.Len() {
  293. // User-supplied EncodeLevel was a no-op. Fall back to strings to keep
  294. // output JSON valid.
  295. final.AppendString(ent.Level.String())
  296. }
  297. }
  298. if final.TimeKey != "" {
  299. final.AddTime(final.TimeKey, ent.Time)
  300. }
  301. if ent.LoggerName != "" && final.NameKey != "" {
  302. final.addKey(final.NameKey)
  303. cur := final.buf.Len()
  304. nameEncoder := final.EncodeName
  305. // if no name encoder provided, fall back to FullNameEncoder for backwards
  306. // compatibility
  307. if nameEncoder == nil {
  308. nameEncoder = FullNameEncoder
  309. }
  310. nameEncoder(ent.LoggerName, final)
  311. if cur == final.buf.Len() {
  312. // User-supplied EncodeName was a no-op. Fall back to strings to
  313. // keep output JSON valid.
  314. final.AppendString(ent.LoggerName)
  315. }
  316. }
  317. if ent.Caller.Defined && final.CallerKey != "" {
  318. final.addKey(final.CallerKey)
  319. cur := final.buf.Len()
  320. final.EncodeCaller(ent.Caller, final)
  321. if cur == final.buf.Len() {
  322. // User-supplied EncodeCaller was a no-op. Fall back to strings to
  323. // keep output JSON valid.
  324. final.AppendString(ent.Caller.String())
  325. }
  326. }
  327. if final.MessageKey != "" {
  328. final.addKey(enc.MessageKey)
  329. final.AppendString(ent.Message)
  330. }
  331. if enc.buf.Len() > 0 {
  332. final.addElementSeparator()
  333. final.buf.Write(enc.buf.Bytes())
  334. }
  335. addFields(final, fields)
  336. final.closeOpenNamespaces()
  337. if ent.Stack != "" && final.StacktraceKey != "" {
  338. final.AddString(final.StacktraceKey, ent.Stack)
  339. }
  340. final.buf.AppendByte('}')
  341. if final.LineEnding != "" {
  342. final.buf.AppendString(final.LineEnding)
  343. } else {
  344. final.buf.AppendString(DefaultLineEnding)
  345. }
  346. ret := final.buf
  347. putJSONEncoder(final)
  348. return ret, nil
  349. }
  350. func (enc *jsonEncoder) truncate() {
  351. enc.buf.Reset()
  352. }
  353. func (enc *jsonEncoder) closeOpenNamespaces() {
  354. for i := 0; i < enc.openNamespaces; i++ {
  355. enc.buf.AppendByte('}')
  356. }
  357. }
  358. func (enc *jsonEncoder) addKey(key string) {
  359. enc.addElementSeparator()
  360. enc.buf.AppendByte('"')
  361. enc.safeAddString(key)
  362. enc.buf.AppendByte('"')
  363. enc.buf.AppendByte(':')
  364. if enc.spaced {
  365. enc.buf.AppendByte(' ')
  366. }
  367. }
  368. func (enc *jsonEncoder) addElementSeparator() {
  369. last := enc.buf.Len() - 1
  370. if last < 0 {
  371. return
  372. }
  373. switch enc.buf.Bytes()[last] {
  374. case '{', '[', ':', ',', ' ':
  375. return
  376. default:
  377. enc.buf.AppendByte(',')
  378. if enc.spaced {
  379. enc.buf.AppendByte(' ')
  380. }
  381. }
  382. }
  383. func (enc *jsonEncoder) appendFloat(val float64, bitSize int) {
  384. enc.addElementSeparator()
  385. switch {
  386. case math.IsNaN(val):
  387. enc.buf.AppendString(`"NaN"`)
  388. case math.IsInf(val, 1):
  389. enc.buf.AppendString(`"+Inf"`)
  390. case math.IsInf(val, -1):
  391. enc.buf.AppendString(`"-Inf"`)
  392. default:
  393. enc.buf.AppendFloat(val, bitSize)
  394. }
  395. }
  396. // safeAddString JSON-escapes a string and appends it to the internal buffer.
  397. // Unlike the standard library's encoder, it doesn't attempt to protect the
  398. // user from browser vulnerabilities or JSONP-related problems.
  399. func (enc *jsonEncoder) safeAddString(s string) {
  400. for i := 0; i < len(s); {
  401. if enc.tryAddRuneSelf(s[i]) {
  402. i++
  403. continue
  404. }
  405. r, size := utf8.DecodeRuneInString(s[i:])
  406. if enc.tryAddRuneError(r, size) {
  407. i++
  408. continue
  409. }
  410. enc.buf.AppendString(s[i : i+size])
  411. i += size
  412. }
  413. }
  414. // safeAddByteString is no-alloc equivalent of safeAddString(string(s)) for s []byte.
  415. func (enc *jsonEncoder) safeAddByteString(s []byte) {
  416. for i := 0; i < len(s); {
  417. if enc.tryAddRuneSelf(s[i]) {
  418. i++
  419. continue
  420. }
  421. r, size := utf8.DecodeRune(s[i:])
  422. if enc.tryAddRuneError(r, size) {
  423. i++
  424. continue
  425. }
  426. enc.buf.Write(s[i : i+size])
  427. i += size
  428. }
  429. }
  430. // tryAddRuneSelf appends b if it is valid UTF-8 character represented in a single byte.
  431. func (enc *jsonEncoder) tryAddRuneSelf(b byte) bool {
  432. if b >= utf8.RuneSelf {
  433. return false
  434. }
  435. if 0x20 <= b && b != '\\' && b != '"' {
  436. enc.buf.AppendByte(b)
  437. return true
  438. }
  439. switch b {
  440. case '\\', '"':
  441. enc.buf.AppendByte('\\')
  442. enc.buf.AppendByte(b)
  443. case '\n':
  444. enc.buf.AppendByte('\\')
  445. enc.buf.AppendByte('n')
  446. case '\r':
  447. enc.buf.AppendByte('\\')
  448. enc.buf.AppendByte('r')
  449. case '\t':
  450. enc.buf.AppendByte('\\')
  451. enc.buf.AppendByte('t')
  452. default:
  453. // Encode bytes < 0x20, except for the escape sequences above.
  454. enc.buf.AppendString(`\u00`)
  455. enc.buf.AppendByte(_hex[b>>4])
  456. enc.buf.AppendByte(_hex[b&0xF])
  457. }
  458. return true
  459. }
  460. func (enc *jsonEncoder) tryAddRuneError(r rune, size int) bool {
  461. if r == utf8.RuneError && size == 1 {
  462. enc.buf.AppendString(`\ufffd`)
  463. return true
  464. }
  465. return false
  466. }