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