zap_text_encoder.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // Copyright 2019 PingCAP, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // Copyright (c) 2016 Uber Technologies, Inc.
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining a copy
  16. // of this software and associated documentation files (the "Software"), to deal
  17. // in the Software without restriction, including without limitation the rights
  18. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. // copies of the Software, and to permit persons to whom the Software is
  20. // furnished to do so, subject to the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be included in
  23. // all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  31. // THE SOFTWARE.
  32. package log
  33. import (
  34. "encoding/base64"
  35. "encoding/json"
  36. "fmt"
  37. "math"
  38. "strings"
  39. "sync"
  40. "time"
  41. "unicode/utf8"
  42. "go.uber.org/zap/buffer"
  43. "go.uber.org/zap/zapcore"
  44. )
  45. // DefaultTimeEncoder serializes time.Time to a human-readable formatted string
  46. func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
  47. s := t.Format("2006/01/02 15:04:05.000 -07:00")
  48. if e, ok := enc.(*textEncoder); ok {
  49. for _, c := range []byte(s) {
  50. e.buf.AppendByte(c)
  51. }
  52. return
  53. }
  54. enc.AppendString(s)
  55. }
  56. // ShortCallerEncoder serializes a caller in file:line format.
  57. func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
  58. enc.AppendString(getCallerString(caller))
  59. }
  60. func getCallerString(ec zapcore.EntryCaller) string {
  61. if !ec.Defined {
  62. return "<unknown>"
  63. }
  64. idx := strings.LastIndexByte(ec.File, '/')
  65. buf := _pool.Get()
  66. for i := idx + 1; i < len(ec.File); i++ {
  67. b := ec.File[i]
  68. switch {
  69. case b >= 'A' && b <= 'Z':
  70. buf.AppendByte(b)
  71. case b >= 'a' && b <= 'z':
  72. buf.AppendByte(b)
  73. case b >= '0' && b <= '9':
  74. buf.AppendByte(b)
  75. case b == '.' || b == '-' || b == '_':
  76. buf.AppendByte(b)
  77. default:
  78. }
  79. }
  80. buf.AppendByte(':')
  81. buf.AppendInt(int64(ec.Line))
  82. caller := buf.String()
  83. buf.Free()
  84. return caller
  85. }
  86. // For JSON-escaping; see textEncoder.safeAddString below.
  87. const _hex = "0123456789abcdef"
  88. var _textPool = sync.Pool{New: func() interface{} {
  89. return &textEncoder{}
  90. }}
  91. var (
  92. _pool = buffer.NewPool()
  93. // Get retrieves a buffer from the pool, creating one if necessary.
  94. Get = _pool.Get
  95. )
  96. func getTextEncoder() *textEncoder {
  97. return _textPool.Get().(*textEncoder)
  98. }
  99. func putTextEncoder(enc *textEncoder) {
  100. if enc.reflectBuf != nil {
  101. enc.reflectBuf.Free()
  102. }
  103. enc.EncoderConfig = nil
  104. enc.buf = nil
  105. enc.spaced = false
  106. enc.openNamespaces = 0
  107. enc.reflectBuf = nil
  108. enc.reflectEnc = nil
  109. _textPool.Put(enc)
  110. }
  111. type textEncoder struct {
  112. *zapcore.EncoderConfig
  113. buf *buffer.Buffer
  114. spaced bool // include spaces after colons and commas
  115. openNamespaces int
  116. disableErrorVerbose bool
  117. // for encoding generic values by reflection
  118. reflectBuf *buffer.Buffer
  119. reflectEnc *json.Encoder
  120. }
  121. // NewTextEncoder creates a fast, low-allocation Text encoder. The encoder
  122. // appropriately escapes all field keys and values.
  123. func NewTextEncoder(cfg *Config) zapcore.Encoder {
  124. cc := zapcore.EncoderConfig{
  125. // Keys can be anything except the empty string.
  126. TimeKey: "time",
  127. LevelKey: "level",
  128. NameKey: "name",
  129. CallerKey: "caller",
  130. MessageKey: "message",
  131. StacktraceKey: "stack",
  132. LineEnding: zapcore.DefaultLineEnding,
  133. EncodeLevel: zapcore.CapitalLevelEncoder,
  134. EncodeTime: DefaultTimeEncoder,
  135. EncodeDuration: zapcore.StringDurationEncoder,
  136. EncodeCaller: ShortCallerEncoder,
  137. }
  138. if cfg.DisableTimestamp {
  139. cc.TimeKey = ""
  140. }
  141. switch cfg.Format {
  142. case "text", "":
  143. return &textEncoder{
  144. EncoderConfig: &cc,
  145. buf: _pool.Get(),
  146. spaced: false,
  147. disableErrorVerbose: cfg.DisableErrorVerbose,
  148. }
  149. case "json":
  150. return zapcore.NewJSONEncoder(cc)
  151. default:
  152. panic(fmt.Sprintf("unsupport log format: %s", cfg.Format))
  153. }
  154. }
  155. func (enc *textEncoder) AddArray(key string, arr zapcore.ArrayMarshaler) error {
  156. enc.addKey(key)
  157. return enc.AppendArray(arr)
  158. }
  159. func (enc *textEncoder) AddObject(key string, obj zapcore.ObjectMarshaler) error {
  160. enc.addKey(key)
  161. return enc.AppendObject(obj)
  162. }
  163. func (enc *textEncoder) AddBinary(key string, val []byte) {
  164. enc.AddString(key, base64.StdEncoding.EncodeToString(val))
  165. }
  166. func (enc *textEncoder) AddByteString(key string, val []byte) {
  167. enc.addKey(key)
  168. enc.AppendByteString(val)
  169. }
  170. func (enc *textEncoder) AddBool(key string, val bool) {
  171. enc.addKey(key)
  172. enc.AppendBool(val)
  173. }
  174. func (enc *textEncoder) AddComplex128(key string, val complex128) {
  175. enc.addKey(key)
  176. enc.AppendComplex128(val)
  177. }
  178. func (enc *textEncoder) AddDuration(key string, val time.Duration) {
  179. enc.addKey(key)
  180. enc.AppendDuration(val)
  181. }
  182. func (enc *textEncoder) AddFloat64(key string, val float64) {
  183. enc.addKey(key)
  184. enc.AppendFloat64(val)
  185. }
  186. func (enc *textEncoder) AddInt64(key string, val int64) {
  187. enc.addKey(key)
  188. enc.AppendInt64(val)
  189. }
  190. func (enc *textEncoder) resetReflectBuf() {
  191. if enc.reflectBuf == nil {
  192. enc.reflectBuf = _pool.Get()
  193. enc.reflectEnc = json.NewEncoder(enc.reflectBuf)
  194. } else {
  195. enc.reflectBuf.Reset()
  196. }
  197. }
  198. func (enc *textEncoder) AddReflected(key string, obj interface{}) error {
  199. enc.resetReflectBuf()
  200. err := enc.reflectEnc.Encode(obj)
  201. if err != nil {
  202. return err
  203. }
  204. enc.reflectBuf.TrimNewline()
  205. enc.addKey(key)
  206. enc.AppendByteString(enc.reflectBuf.Bytes())
  207. return nil
  208. }
  209. func (enc *textEncoder) OpenNamespace(key string) {
  210. enc.addKey(key)
  211. enc.buf.AppendByte('{')
  212. enc.openNamespaces++
  213. }
  214. func (enc *textEncoder) AddString(key, val string) {
  215. enc.addKey(key)
  216. enc.AppendString(val)
  217. }
  218. func (enc *textEncoder) AddTime(key string, val time.Time) {
  219. enc.addKey(key)
  220. enc.AppendTime(val)
  221. }
  222. func (enc *textEncoder) AddUint64(key string, val uint64) {
  223. enc.addKey(key)
  224. enc.AppendUint64(val)
  225. }
  226. func (enc *textEncoder) AppendArray(arr zapcore.ArrayMarshaler) error {
  227. enc.addElementSeparator()
  228. ne := enc.cloned()
  229. ne.buf.AppendByte('[')
  230. err := arr.MarshalLogArray(ne)
  231. ne.buf.AppendByte(']')
  232. enc.AppendByteString(ne.buf.Bytes())
  233. ne.buf.Free()
  234. putTextEncoder(ne)
  235. return err
  236. }
  237. func (enc *textEncoder) AppendObject(obj zapcore.ObjectMarshaler) error {
  238. enc.addElementSeparator()
  239. ne := enc.cloned()
  240. ne.buf.AppendByte('{')
  241. err := obj.MarshalLogObject(ne)
  242. ne.buf.AppendByte('}')
  243. enc.AppendByteString(ne.buf.Bytes())
  244. ne.buf.Free()
  245. putTextEncoder(ne)
  246. return err
  247. }
  248. func (enc *textEncoder) AppendBool(val bool) {
  249. enc.addElementSeparator()
  250. enc.buf.AppendBool(val)
  251. }
  252. func (enc *textEncoder) AppendByteString(val []byte) {
  253. enc.addElementSeparator()
  254. if !enc.needDoubleQuotes(string(val)) {
  255. enc.safeAddByteString(val)
  256. return
  257. }
  258. enc.buf.AppendByte('"')
  259. enc.safeAddByteString(val)
  260. enc.buf.AppendByte('"')
  261. }
  262. func (enc *textEncoder) AppendComplex128(val complex128) {
  263. enc.addElementSeparator()
  264. // Cast to a platform-independent, fixed-size type.
  265. r, i := float64(real(val)), float64(imag(val))
  266. enc.buf.AppendFloat(r, 64)
  267. enc.buf.AppendByte('+')
  268. enc.buf.AppendFloat(i, 64)
  269. enc.buf.AppendByte('i')
  270. }
  271. func (enc *textEncoder) AppendDuration(val time.Duration) {
  272. cur := enc.buf.Len()
  273. enc.EncodeDuration(val, enc)
  274. if cur == enc.buf.Len() {
  275. // User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
  276. // JSON valid.
  277. enc.AppendInt64(int64(val))
  278. }
  279. }
  280. func (enc *textEncoder) AppendInt64(val int64) {
  281. enc.addElementSeparator()
  282. enc.buf.AppendInt(val)
  283. }
  284. func (enc *textEncoder) AppendReflected(val interface{}) error {
  285. enc.resetReflectBuf()
  286. err := enc.reflectEnc.Encode(val)
  287. if err != nil {
  288. return err
  289. }
  290. enc.reflectBuf.TrimNewline()
  291. enc.AppendByteString(enc.reflectBuf.Bytes())
  292. return nil
  293. }
  294. func (enc *textEncoder) AppendString(val string) {
  295. enc.addElementSeparator()
  296. enc.safeAddStringWithQuote(val)
  297. }
  298. func (enc *textEncoder) AppendTime(val time.Time) {
  299. cur := enc.buf.Len()
  300. enc.EncodeTime(val, enc)
  301. if cur == enc.buf.Len() {
  302. // User-supplied EncodeTime is a no-op. Fall back to nanos since epoch to keep
  303. // output JSON valid.
  304. enc.AppendInt64(val.UnixNano())
  305. }
  306. }
  307. func (enc *textEncoder) beginQuoteFiled() {
  308. if enc.buf.Len() > 0 {
  309. enc.buf.AppendByte(' ')
  310. }
  311. enc.buf.AppendByte('[')
  312. }
  313. func (enc *textEncoder) endQuoteFiled() {
  314. enc.buf.AppendByte(']')
  315. }
  316. func (enc *textEncoder) AppendUint64(val uint64) {
  317. enc.addElementSeparator()
  318. enc.buf.AppendUint(val)
  319. }
  320. func (enc *textEncoder) AddComplex64(k string, v complex64) { enc.AddComplex128(k, complex128(v)) }
  321. func (enc *textEncoder) AddFloat32(k string, v float32) { enc.AddFloat64(k, float64(v)) }
  322. func (enc *textEncoder) AddInt(k string, v int) { enc.AddInt64(k, int64(v)) }
  323. func (enc *textEncoder) AddInt32(k string, v int32) { enc.AddInt64(k, int64(v)) }
  324. func (enc *textEncoder) AddInt16(k string, v int16) { enc.AddInt64(k, int64(v)) }
  325. func (enc *textEncoder) AddInt8(k string, v int8) { enc.AddInt64(k, int64(v)) }
  326. func (enc *textEncoder) AddUint(k string, v uint) { enc.AddUint64(k, uint64(v)) }
  327. func (enc *textEncoder) AddUint32(k string, v uint32) { enc.AddUint64(k, uint64(v)) }
  328. func (enc *textEncoder) AddUint16(k string, v uint16) { enc.AddUint64(k, uint64(v)) }
  329. func (enc *textEncoder) AddUint8(k string, v uint8) { enc.AddUint64(k, uint64(v)) }
  330. func (enc *textEncoder) AddUintptr(k string, v uintptr) { enc.AddUint64(k, uint64(v)) }
  331. func (enc *textEncoder) AppendComplex64(v complex64) { enc.AppendComplex128(complex128(v)) }
  332. func (enc *textEncoder) AppendFloat64(v float64) { enc.appendFloat(v, 64) }
  333. func (enc *textEncoder) AppendFloat32(v float32) { enc.appendFloat(float64(v), 32) }
  334. func (enc *textEncoder) AppendInt(v int) { enc.AppendInt64(int64(v)) }
  335. func (enc *textEncoder) AppendInt32(v int32) { enc.AppendInt64(int64(v)) }
  336. func (enc *textEncoder) AppendInt16(v int16) { enc.AppendInt64(int64(v)) }
  337. func (enc *textEncoder) AppendInt8(v int8) { enc.AppendInt64(int64(v)) }
  338. func (enc *textEncoder) AppendUint(v uint) { enc.AppendUint64(uint64(v)) }
  339. func (enc *textEncoder) AppendUint32(v uint32) { enc.AppendUint64(uint64(v)) }
  340. func (enc *textEncoder) AppendUint16(v uint16) { enc.AppendUint64(uint64(v)) }
  341. func (enc *textEncoder) AppendUint8(v uint8) { enc.AppendUint64(uint64(v)) }
  342. func (enc *textEncoder) AppendUintptr(v uintptr) { enc.AppendUint64(uint64(v)) }
  343. func (enc *textEncoder) Clone() zapcore.Encoder {
  344. clone := enc.cloned()
  345. clone.buf.Write(enc.buf.Bytes())
  346. return clone
  347. }
  348. func (enc *textEncoder) cloned() *textEncoder {
  349. clone := getTextEncoder()
  350. clone.EncoderConfig = enc.EncoderConfig
  351. clone.spaced = enc.spaced
  352. clone.openNamespaces = enc.openNamespaces
  353. clone.disableErrorVerbose = enc.disableErrorVerbose
  354. clone.buf = _pool.Get()
  355. return clone
  356. }
  357. func (enc *textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
  358. final := enc.cloned()
  359. if final.TimeKey != "" {
  360. final.beginQuoteFiled()
  361. final.AppendTime(ent.Time)
  362. final.endQuoteFiled()
  363. }
  364. if final.LevelKey != "" {
  365. final.beginQuoteFiled()
  366. cur := final.buf.Len()
  367. final.EncodeLevel(ent.Level, final)
  368. if cur == final.buf.Len() {
  369. // User-supplied EncodeLevel was a no-op. Fall back to strings to keep
  370. // output JSON valid.
  371. final.AppendString(ent.Level.String())
  372. }
  373. final.endQuoteFiled()
  374. }
  375. if ent.LoggerName != "" && final.NameKey != "" {
  376. final.beginQuoteFiled()
  377. cur := final.buf.Len()
  378. nameEncoder := final.EncodeName
  379. // if no name encoder provided, fall back to FullNameEncoder for backwards
  380. // compatibility
  381. if nameEncoder == nil {
  382. nameEncoder = zapcore.FullNameEncoder
  383. }
  384. nameEncoder(ent.LoggerName, final)
  385. if cur == final.buf.Len() {
  386. // User-supplied EncodeName was a no-op. Fall back to strings to
  387. // keep output JSON valid.
  388. final.AppendString(ent.LoggerName)
  389. }
  390. final.endQuoteFiled()
  391. }
  392. if ent.Caller.Defined && final.CallerKey != "" {
  393. final.beginQuoteFiled()
  394. cur := final.buf.Len()
  395. final.EncodeCaller(ent.Caller, final)
  396. if cur == final.buf.Len() {
  397. // User-supplied EncodeCaller was a no-op. Fall back to strings to
  398. // keep output JSON valid.
  399. final.AppendString(ent.Caller.String())
  400. }
  401. final.endQuoteFiled()
  402. }
  403. // add Message
  404. if len(ent.Message) > 0 {
  405. final.beginQuoteFiled()
  406. final.AppendString(ent.Message)
  407. final.endQuoteFiled()
  408. }
  409. if enc.buf.Len() > 0 {
  410. final.buf.AppendByte(' ')
  411. final.buf.Write(enc.buf.Bytes())
  412. }
  413. final.addFields(fields)
  414. final.closeOpenNamespaces()
  415. if ent.Stack != "" && final.StacktraceKey != "" {
  416. final.beginQuoteFiled()
  417. final.AddString(final.StacktraceKey, ent.Stack)
  418. final.endQuoteFiled()
  419. }
  420. if final.LineEnding != "" {
  421. final.buf.AppendString(final.LineEnding)
  422. } else {
  423. final.buf.AppendString(zapcore.DefaultLineEnding)
  424. }
  425. ret := final.buf
  426. putTextEncoder(final)
  427. return ret, nil
  428. }
  429. func (enc *textEncoder) truncate() {
  430. enc.buf.Reset()
  431. }
  432. func (enc *textEncoder) closeOpenNamespaces() {
  433. for i := 0; i < enc.openNamespaces; i++ {
  434. enc.buf.AppendByte('}')
  435. }
  436. }
  437. func (enc *textEncoder) addKey(key string) {
  438. enc.addElementSeparator()
  439. enc.safeAddStringWithQuote(key)
  440. enc.buf.AppendByte('=')
  441. }
  442. func (enc *textEncoder) addElementSeparator() {
  443. last := enc.buf.Len() - 1
  444. if last < 0 {
  445. return
  446. }
  447. switch enc.buf.Bytes()[last] {
  448. case '{', '[', ':', ',', ' ', '=':
  449. return
  450. default:
  451. enc.buf.AppendByte(',')
  452. }
  453. }
  454. func (enc *textEncoder) appendFloat(val float64, bitSize int) {
  455. enc.addElementSeparator()
  456. switch {
  457. case math.IsNaN(val):
  458. enc.buf.AppendString("NaN")
  459. case math.IsInf(val, 1):
  460. enc.buf.AppendString("+Inf")
  461. case math.IsInf(val, -1):
  462. enc.buf.AppendString("-Inf")
  463. default:
  464. enc.buf.AppendFloat(val, bitSize)
  465. }
  466. }
  467. // safeAddString JSON-escapes a string and appends it to the internal buffer.
  468. // Unlike the standard library's encoder, it doesn't attempt to protect the
  469. // user from browser vulnerabilities or JSONP-related problems.
  470. func (enc *textEncoder) safeAddString(s string) {
  471. for i := 0; i < len(s); {
  472. if enc.tryAddRuneSelf(s[i]) {
  473. i++
  474. continue
  475. }
  476. r, size := utf8.DecodeRuneInString(s[i:])
  477. if enc.tryAddRuneError(r, size) {
  478. i++
  479. continue
  480. }
  481. enc.buf.AppendString(s[i : i+size])
  482. i += size
  483. }
  484. }
  485. // safeAddStringWithQuote will automatically add quotoes.
  486. func (enc *textEncoder) safeAddStringWithQuote(s string) {
  487. if !enc.needDoubleQuotes(s) {
  488. enc.safeAddString(s)
  489. return
  490. }
  491. enc.buf.AppendByte('"')
  492. enc.safeAddString(s)
  493. enc.buf.AppendByte('"')
  494. }
  495. // safeAddByteString is no-alloc equivalent of safeAddString(string(s)) for s []byte.
  496. func (enc *textEncoder) safeAddByteString(s []byte) {
  497. for i := 0; i < len(s); {
  498. if enc.tryAddRuneSelf(s[i]) {
  499. i++
  500. continue
  501. }
  502. r, size := utf8.DecodeRune(s[i:])
  503. if enc.tryAddRuneError(r, size) {
  504. i++
  505. continue
  506. }
  507. enc.buf.Write(s[i : i+size])
  508. i += size
  509. }
  510. }
  511. // See [log-fileds](https://github.com/tikv/rfcs/blob/master/text/2018-12-19-unified-log-format.md#log-fields-section).
  512. func (enc *textEncoder) needDoubleQuotes(s string) bool {
  513. for i := 0; i < len(s); {
  514. b := s[i]
  515. if b <= 0x20 {
  516. return true
  517. }
  518. switch b {
  519. case '\\', '"', '[', ']', '=':
  520. return true
  521. }
  522. i++
  523. }
  524. return false
  525. }
  526. // tryAddRuneSelf appends b if it is valid UTF-8 character represented in a single byte.
  527. func (enc *textEncoder) tryAddRuneSelf(b byte) bool {
  528. if b >= utf8.RuneSelf {
  529. return false
  530. }
  531. if 0x20 <= b && b != '\\' && b != '"' {
  532. enc.buf.AppendByte(b)
  533. return true
  534. }
  535. switch b {
  536. case '\\', '"':
  537. enc.buf.AppendByte('\\')
  538. enc.buf.AppendByte(b)
  539. case '\n':
  540. enc.buf.AppendByte('\\')
  541. enc.buf.AppendByte('n')
  542. case '\r':
  543. enc.buf.AppendByte('\\')
  544. enc.buf.AppendByte('r')
  545. case '\t':
  546. enc.buf.AppendByte('\\')
  547. enc.buf.AppendByte('t')
  548. default:
  549. // Encode bytes < 0x20, except for the escape sequences above.
  550. enc.buf.AppendString(`\u00`)
  551. enc.buf.AppendByte(_hex[b>>4])
  552. enc.buf.AppendByte(_hex[b&0xF])
  553. }
  554. return true
  555. }
  556. func (enc *textEncoder) tryAddRuneError(r rune, size int) bool {
  557. if r == utf8.RuneError && size == 1 {
  558. enc.buf.AppendString(`\ufffd`)
  559. return true
  560. }
  561. return false
  562. }
  563. func (enc *textEncoder) addFields(fields []zapcore.Field) {
  564. for _, f := range fields {
  565. if f.Type == zapcore.ErrorType {
  566. // handle ErrorType in pingcap/log to fix "[key=?,keyVerbose=?]" problem.
  567. // see more detail at https://github.com/pingcap/log/pull/5
  568. enc.encodeError(f)
  569. continue
  570. }
  571. enc.beginQuoteFiled()
  572. f.AddTo(enc)
  573. enc.endQuoteFiled()
  574. }
  575. }
  576. func (enc *textEncoder) encodeError(f zapcore.Field) {
  577. err := f.Interface.(error)
  578. basic := err.Error()
  579. enc.beginQuoteFiled()
  580. enc.AddString(f.Key, basic)
  581. enc.endQuoteFiled()
  582. if enc.disableErrorVerbose {
  583. return
  584. }
  585. if e, isFormatter := err.(fmt.Formatter); isFormatter {
  586. verbose := fmt.Sprintf("%+v", e)
  587. if verbose != basic {
  588. // This is a rich error type, like those produced by
  589. // github.com/pkg/errors.
  590. enc.beginQuoteFiled()
  591. enc.AddString(f.Key+"Verbose", verbose)
  592. enc.endQuoteFiled()
  593. }
  594. }
  595. }