1 // Copyright (c) 2016 Uber Technologies, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
31 "go.uber.org/zap/buffer"
32 "go.uber.org/zap/internal/bufferpool"
35 // For JSON-escaping; see jsonEncoder.safeAddString below.
36 const _hex = "0123456789abcdef"
38 var _jsonPool = sync.Pool{New: func() interface{} {
42 func getJSONEncoder() *jsonEncoder {
43 return _jsonPool.Get().(*jsonEncoder)
46 func putJSONEncoder(enc *jsonEncoder) {
47 if enc.reflectBuf != nil {
50 enc.EncoderConfig = nil
53 enc.openNamespaces = 0
59 type jsonEncoder struct {
62 spaced bool // include spaces after colons and commas
65 // for encoding generic values by reflection
66 reflectBuf *buffer.Buffer
67 reflectEnc *json.Encoder
70 // NewJSONEncoder creates a fast, low-allocation JSON encoder. The encoder
71 // appropriately escapes all field keys and values.
73 // Note that the encoder doesn't deduplicate keys, so it's possible to produce
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
80 func NewJSONEncoder(cfg EncoderConfig) Encoder {
81 return newJSONEncoder(cfg, false)
84 func newJSONEncoder(cfg EncoderConfig, spaced bool) *jsonEncoder {
87 buf: bufferpool.Get(),
92 func (enc *jsonEncoder) AddArray(key string, arr ArrayMarshaler) error {
94 return enc.AppendArray(arr)
97 func (enc *jsonEncoder) AddObject(key string, obj ObjectMarshaler) error {
99 return enc.AppendObject(obj)
102 func (enc *jsonEncoder) AddBinary(key string, val []byte) {
103 enc.AddString(key, base64.StdEncoding.EncodeToString(val))
106 func (enc *jsonEncoder) AddByteString(key string, val []byte) {
108 enc.AppendByteString(val)
111 func (enc *jsonEncoder) AddBool(key string, val bool) {
116 func (enc *jsonEncoder) AddComplex128(key string, val complex128) {
118 enc.AppendComplex128(val)
121 func (enc *jsonEncoder) AddDuration(key string, val time.Duration) {
123 enc.AppendDuration(val)
126 func (enc *jsonEncoder) AddFloat64(key string, val float64) {
128 enc.AppendFloat64(val)
131 func (enc *jsonEncoder) AddInt64(key string, val int64) {
136 func (enc *jsonEncoder) resetReflectBuf() {
137 if enc.reflectBuf == nil {
138 enc.reflectBuf = bufferpool.Get()
139 enc.reflectEnc = json.NewEncoder(enc.reflectBuf)
141 enc.reflectBuf.Reset()
145 func (enc *jsonEncoder) AddReflected(key string, obj interface{}) error {
146 enc.resetReflectBuf()
147 err := enc.reflectEnc.Encode(obj)
151 enc.reflectBuf.TrimNewline()
153 _, err = enc.buf.Write(enc.reflectBuf.Bytes())
157 func (enc *jsonEncoder) OpenNamespace(key string) {
159 enc.buf.AppendByte('{')
163 func (enc *jsonEncoder) AddString(key, val string) {
165 enc.AppendString(val)
168 func (enc *jsonEncoder) AddTime(key string, val time.Time) {
173 func (enc *jsonEncoder) AddUint64(key string, val uint64) {
175 enc.AppendUint64(val)
178 func (enc *jsonEncoder) AppendArray(arr ArrayMarshaler) error {
179 enc.addElementSeparator()
180 enc.buf.AppendByte('[')
181 err := arr.MarshalLogArray(enc)
182 enc.buf.AppendByte(']')
186 func (enc *jsonEncoder) AppendObject(obj ObjectMarshaler) error {
187 enc.addElementSeparator()
188 enc.buf.AppendByte('{')
189 err := obj.MarshalLogObject(enc)
190 enc.buf.AppendByte('}')
194 func (enc *jsonEncoder) AppendBool(val bool) {
195 enc.addElementSeparator()
196 enc.buf.AppendBool(val)
199 func (enc *jsonEncoder) AppendByteString(val []byte) {
200 enc.addElementSeparator()
201 enc.buf.AppendByte('"')
202 enc.safeAddByteString(val)
203 enc.buf.AppendByte('"')
206 func (enc *jsonEncoder) AppendComplex128(val complex128) {
207 enc.addElementSeparator()
208 // Cast to a platform-independent, fixed-size type.
209 r, i := float64(real(val)), float64(imag(val))
210 enc.buf.AppendByte('"')
211 // Because we're always in a quoted string, we can use strconv without
212 // special-casing NaN and +/-Inf.
213 enc.buf.AppendFloat(r, 64)
214 enc.buf.AppendByte('+')
215 enc.buf.AppendFloat(i, 64)
216 enc.buf.AppendByte('i')
217 enc.buf.AppendByte('"')
220 func (enc *jsonEncoder) AppendDuration(val time.Duration) {
222 enc.EncodeDuration(val, enc)
223 if cur == enc.buf.Len() {
224 // User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
226 enc.AppendInt64(int64(val))
230 func (enc *jsonEncoder) AppendInt64(val int64) {
231 enc.addElementSeparator()
232 enc.buf.AppendInt(val)
235 func (enc *jsonEncoder) AppendReflected(val interface{}) error {
236 enc.resetReflectBuf()
237 err := enc.reflectEnc.Encode(val)
241 enc.reflectBuf.TrimNewline()
242 enc.addElementSeparator()
243 _, err = enc.buf.Write(enc.reflectBuf.Bytes())
247 func (enc *jsonEncoder) AppendString(val string) {
248 enc.addElementSeparator()
249 enc.buf.AppendByte('"')
250 enc.safeAddString(val)
251 enc.buf.AppendByte('"')
254 func (enc *jsonEncoder) AppendTime(val time.Time) {
256 enc.EncodeTime(val, enc)
257 if cur == enc.buf.Len() {
258 // User-supplied EncodeTime is a no-op. Fall back to nanos since epoch to keep
259 // output JSON valid.
260 enc.AppendInt64(val.UnixNano())
264 func (enc *jsonEncoder) AppendUint64(val uint64) {
265 enc.addElementSeparator()
266 enc.buf.AppendUint(val)
269 func (enc *jsonEncoder) AddComplex64(k string, v complex64) { enc.AddComplex128(k, complex128(v)) }
270 func (enc *jsonEncoder) AddFloat32(k string, v float32) { enc.AddFloat64(k, float64(v)) }
271 func (enc *jsonEncoder) AddInt(k string, v int) { enc.AddInt64(k, int64(v)) }
272 func (enc *jsonEncoder) AddInt32(k string, v int32) { enc.AddInt64(k, int64(v)) }
273 func (enc *jsonEncoder) AddInt16(k string, v int16) { enc.AddInt64(k, int64(v)) }
274 func (enc *jsonEncoder) AddInt8(k string, v int8) { enc.AddInt64(k, int64(v)) }
275 func (enc *jsonEncoder) AddUint(k string, v uint) { enc.AddUint64(k, uint64(v)) }
276 func (enc *jsonEncoder) AddUint32(k string, v uint32) { enc.AddUint64(k, uint64(v)) }
277 func (enc *jsonEncoder) AddUint16(k string, v uint16) { enc.AddUint64(k, uint64(v)) }
278 func (enc *jsonEncoder) AddUint8(k string, v uint8) { enc.AddUint64(k, uint64(v)) }
279 func (enc *jsonEncoder) AddUintptr(k string, v uintptr) { enc.AddUint64(k, uint64(v)) }
280 func (enc *jsonEncoder) AppendComplex64(v complex64) { enc.AppendComplex128(complex128(v)) }
281 func (enc *jsonEncoder) AppendFloat64(v float64) { enc.appendFloat(v, 64) }
282 func (enc *jsonEncoder) AppendFloat32(v float32) { enc.appendFloat(float64(v), 32) }
283 func (enc *jsonEncoder) AppendInt(v int) { enc.AppendInt64(int64(v)) }
284 func (enc *jsonEncoder) AppendInt32(v int32) { enc.AppendInt64(int64(v)) }
285 func (enc *jsonEncoder) AppendInt16(v int16) { enc.AppendInt64(int64(v)) }
286 func (enc *jsonEncoder) AppendInt8(v int8) { enc.AppendInt64(int64(v)) }
287 func (enc *jsonEncoder) AppendUint(v uint) { enc.AppendUint64(uint64(v)) }
288 func (enc *jsonEncoder) AppendUint32(v uint32) { enc.AppendUint64(uint64(v)) }
289 func (enc *jsonEncoder) AppendUint16(v uint16) { enc.AppendUint64(uint64(v)) }
290 func (enc *jsonEncoder) AppendUint8(v uint8) { enc.AppendUint64(uint64(v)) }
291 func (enc *jsonEncoder) AppendUintptr(v uintptr) { enc.AppendUint64(uint64(v)) }
293 func (enc *jsonEncoder) Clone() Encoder {
295 clone.buf.Write(enc.buf.Bytes())
299 func (enc *jsonEncoder) clone() *jsonEncoder {
300 clone := getJSONEncoder()
301 clone.EncoderConfig = enc.EncoderConfig
302 clone.spaced = enc.spaced
303 clone.openNamespaces = enc.openNamespaces
304 clone.buf = bufferpool.Get()
308 func (enc *jsonEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, error) {
310 final.buf.AppendByte('{')
312 if final.LevelKey != "" {
313 final.addKey(final.LevelKey)
314 cur := final.buf.Len()
315 final.EncodeLevel(ent.Level, final)
316 if cur == final.buf.Len() {
317 // User-supplied EncodeLevel was a no-op. Fall back to strings to keep
318 // output JSON valid.
319 final.AppendString(ent.Level.String())
322 if final.TimeKey != "" {
323 final.AddTime(final.TimeKey, ent.Time)
325 if ent.LoggerName != "" && final.NameKey != "" {
326 final.addKey(final.NameKey)
327 cur := final.buf.Len()
328 nameEncoder := final.EncodeName
330 // if no name encoder provided, fall back to FullNameEncoder for backwards
332 if nameEncoder == nil {
333 nameEncoder = FullNameEncoder
336 nameEncoder(ent.LoggerName, final)
337 if cur == final.buf.Len() {
338 // User-supplied EncodeName was a no-op. Fall back to strings to
339 // keep output JSON valid.
340 final.AppendString(ent.LoggerName)
343 if ent.Caller.Defined && final.CallerKey != "" {
344 final.addKey(final.CallerKey)
345 cur := final.buf.Len()
346 final.EncodeCaller(ent.Caller, final)
347 if cur == final.buf.Len() {
348 // User-supplied EncodeCaller was a no-op. Fall back to strings to
349 // keep output JSON valid.
350 final.AppendString(ent.Caller.String())
353 if final.MessageKey != "" {
354 final.addKey(enc.MessageKey)
355 final.AppendString(ent.Message)
357 if enc.buf.Len() > 0 {
358 final.addElementSeparator()
359 final.buf.Write(enc.buf.Bytes())
361 addFields(final, fields)
362 final.closeOpenNamespaces()
363 if ent.Stack != "" && final.StacktraceKey != "" {
364 final.AddString(final.StacktraceKey, ent.Stack)
366 final.buf.AppendByte('}')
367 if final.LineEnding != "" {
368 final.buf.AppendString(final.LineEnding)
370 final.buf.AppendString(DefaultLineEnding)
374 putJSONEncoder(final)
378 func (enc *jsonEncoder) truncate() {
382 func (enc *jsonEncoder) closeOpenNamespaces() {
383 for i := 0; i < enc.openNamespaces; i++ {
384 enc.buf.AppendByte('}')
388 func (enc *jsonEncoder) addKey(key string) {
389 enc.addElementSeparator()
390 enc.buf.AppendByte('"')
391 enc.safeAddString(key)
392 enc.buf.AppendByte('"')
393 enc.buf.AppendByte(':')
395 enc.buf.AppendByte(' ')
399 func (enc *jsonEncoder) addElementSeparator() {
400 last := enc.buf.Len() - 1
404 switch enc.buf.Bytes()[last] {
405 case '{', '[', ':', ',', ' ':
408 enc.buf.AppendByte(',')
410 enc.buf.AppendByte(' ')
415 func (enc *jsonEncoder) appendFloat(val float64, bitSize int) {
416 enc.addElementSeparator()
418 case math.IsNaN(val):
419 enc.buf.AppendString(`"NaN"`)
420 case math.IsInf(val, 1):
421 enc.buf.AppendString(`"+Inf"`)
422 case math.IsInf(val, -1):
423 enc.buf.AppendString(`"-Inf"`)
425 enc.buf.AppendFloat(val, bitSize)
429 // safeAddString JSON-escapes a string and appends it to the internal buffer.
430 // Unlike the standard library's encoder, it doesn't attempt to protect the
431 // user from browser vulnerabilities or JSONP-related problems.
432 func (enc *jsonEncoder) safeAddString(s string) {
433 for i := 0; i < len(s); {
434 if enc.tryAddRuneSelf(s[i]) {
438 r, size := utf8.DecodeRuneInString(s[i:])
439 if enc.tryAddRuneError(r, size) {
443 enc.buf.AppendString(s[i : i+size])
448 // safeAddByteString is no-alloc equivalent of safeAddString(string(s)) for s []byte.
449 func (enc *jsonEncoder) safeAddByteString(s []byte) {
450 for i := 0; i < len(s); {
451 if enc.tryAddRuneSelf(s[i]) {
455 r, size := utf8.DecodeRune(s[i:])
456 if enc.tryAddRuneError(r, size) {
460 enc.buf.Write(s[i : i+size])
465 // tryAddRuneSelf appends b if it is valid UTF-8 character represented in a single byte.
466 func (enc *jsonEncoder) tryAddRuneSelf(b byte) bool {
467 if b >= utf8.RuneSelf {
470 if 0x20 <= b && b != '\\' && b != '"' {
471 enc.buf.AppendByte(b)
476 enc.buf.AppendByte('\\')
477 enc.buf.AppendByte(b)
479 enc.buf.AppendByte('\\')
480 enc.buf.AppendByte('n')
482 enc.buf.AppendByte('\\')
483 enc.buf.AppendByte('r')
485 enc.buf.AppendByte('\\')
486 enc.buf.AppendByte('t')
488 // Encode bytes < 0x20, except for the escape sequences above.
489 enc.buf.AppendString(`\u00`)
490 enc.buf.AppendByte(_hex[b>>4])
491 enc.buf.AppendByte(_hex[b&0xF])
496 func (enc *jsonEncoder) tryAddRuneError(r rune, size int) bool {
497 if r == utf8.RuneError && size == 1 {
498 enc.buf.AppendString(`\ufffd`)