ea1184be2b
benchstat old new name old time/op new time/op delta LogEmpty-8 15.2ns ±14% 13.4ns ± 3% -12.11% (p=0.008 n=5+5) Disabled-8 2.50ns ± 1% 2.28ns ± 6% -8.81% (p=0.008 n=5+5) Info-8 44.4ns ± 1% 36.4ns ± 4% -17.99% (p=0.008 n=5+5) ContextFields-8 47.6ns ± 1% 39.4ns ± 7% -17.30% (p=0.008 n=5+5) ContextAppend-8 18.9ns ± 4% 15.2ns ± 4% -19.68% (p=0.008 n=5+5) LogFields-8 181ns ± 2% 173ns ± 2% -4.63% (p=0.008 n=5+5) LogArrayObject-8 530ns ± 3% 487ns ± 3% -8.11% (p=0.008 n=5+5) LogFieldType/Int-8 29.5ns ± 3% 28.8ns ± 2% ~ (p=0.167 n=5+5) LogFieldType/Interface-8 180ns ± 7% 175ns ± 4% ~ (p=0.579 n=5+5) LogFieldType/Interface(Object)-8 87.8ns ± 3% 80.5ns ± 1% -8.29% (p=0.008 n=5+5) LogFieldType/Object-8 83.7ns ± 2% 77.2ns ± 3% -7.76% (p=0.008 n=5+5) LogFieldType/Bools-8 34.6ns ± 3% 32.3ns ± 6% -6.64% (p=0.032 n=5+5) LogFieldType/Float-8 43.0ns ± 4% 40.5ns ± 4% -5.86% (p=0.016 n=5+5) LogFieldType/Str-8 29.8ns ± 2% 26.5ns ± 5% -11.01% (p=0.008 n=5+5) LogFieldType/Err-8 32.8ns ± 2% 29.8ns ± 4% -9.21% (p=0.008 n=5+5) LogFieldType/Durs-8 309ns ± 3% 304ns ± 3% ~ (p=0.238 n=5+5) LogFieldType/Floats-8 175ns ± 2% 174ns ± 3% ~ (p=0.968 n=5+5) LogFieldType/Strs-8 51.0ns ± 3% 48.4ns ± 6% -5.06% (p=0.032 n=5+5) LogFieldType/Dur-8 44.5ns ± 3% 41.3ns ± 3% -7.11% (p=0.008 n=5+5) LogFieldType/Interface(Objects)-8 758ns ± 3% 760ns ± 6% ~ (p=1.000 n=5+5) LogFieldType/Interfaces-8 772ns ± 5% 762ns ± 4% ~ (p=0.794 n=5+5) LogFieldType/Bool-8 28.0ns ± 6% 26.5ns ± 9% ~ (p=0.143 n=5+5) LogFieldType/Ints-8 49.6ns ± 2% 46.2ns ± 2% -6.70% (p=0.008 n=5+5) LogFieldType/Errs-8 46.5ns ±11% 40.9ns ± 4% -11.92% (p=0.008 n=5+5) LogFieldType/Time-8 115ns ± 3% 113ns ± 3% ~ (p=0.167 n=5+5) LogFieldType/Times-8 810ns ± 1% 811ns ± 3% ~ (p=0.889 n=5+5) ContextFieldType/Errs-8 158ns ± 6% 156ns ±12% ~ (p=1.000 n=5+5) ContextFieldType/Times-8 165ns ±11% 173ns ± 9% ~ (p=0.651 n=5+5) ContextFieldType/Interface-8 289ns ±13% 287ns ±11% ~ (p=0.690 n=5+5) ContextFieldType/Interface(Object)-8 285ns ±12% 297ns ± 6% ~ (p=0.238 n=5+5) ContextFieldType/Interface(Objects)-8 941ns ± 6% 941ns ± 5% ~ (p=1.000 n=5+5) ContextFieldType/Object-8 201ns ± 5% 210ns ±12% ~ (p=0.262 n=5+5) ContextFieldType/Ints-8 173ns ±10% 165ns ± 9% ~ (p=0.198 n=5+5) ContextFieldType/Floats-8 297ns ± 6% 292ns ± 7% ~ (p=0.579 n=5+5) ContextFieldType/Timestamp-8 174ns ± 9% 174ns ±11% ~ (p=0.810 n=5+5) ContextFieldType/Durs-8 445ns ± 9% 425ns ± 3% ~ (p=0.151 n=5+5) ContextFieldType/Interfaces-8 944ns ± 6% 876ns ±10% ~ (p=0.095 n=5+5) ContextFieldType/Strs-8 179ns ±11% 165ns ±13% ~ (p=0.135 n=5+5) ContextFieldType/Dur-8 158ns ± 8% 160ns ±19% ~ (p=1.000 n=5+5) ContextFieldType/Time-8 152ns ±15% 148ns ±14% ~ (p=0.952 n=5+5) ContextFieldType/Str-8 146ns ±12% 147ns ±16% ~ (p=0.841 n=5+5) ContextFieldType/Err-8 138ns ±12% 145ns ±17% ~ (p=0.595 n=5+5) ContextFieldType/Int-8 145ns ±10% 146ns ±13% ~ (p=0.873 n=5+5) ContextFieldType/Float-8 181ns ± 9% 162ns ±12% ~ (p=0.151 n=5+5) ContextFieldType/Bool-8 153ns ±10% 131ns ±19% ~ (p=0.063 n=5+5) ContextFieldType/Bools-8 149ns ±11% 160ns ±16% ~ (p=0.500 n=5+5)
403 lines
12 KiB
Go
403 lines
12 KiB
Go
package json
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"net"
|
|
"strconv"
|
|
)
|
|
|
|
// AppendNil inserts a 'Nil' object into the dst byte array.
|
|
func (Encoder) AppendNil(dst []byte) []byte {
|
|
return append(dst, "null"...)
|
|
}
|
|
|
|
// AppendBeginMarker inserts a map start into the dst byte array.
|
|
func (Encoder) AppendBeginMarker(dst []byte) []byte {
|
|
return append(dst, '{')
|
|
}
|
|
|
|
// AppendEndMarker inserts a map end into the dst byte array.
|
|
func (Encoder) AppendEndMarker(dst []byte) []byte {
|
|
return append(dst, '}')
|
|
}
|
|
|
|
// AppendLineBreak appends a line break.
|
|
func (Encoder) AppendLineBreak(dst []byte) []byte {
|
|
return append(dst, '\n')
|
|
}
|
|
|
|
// AppendArrayStart adds markers to indicate the start of an array.
|
|
func (Encoder) AppendArrayStart(dst []byte) []byte {
|
|
return append(dst, '[')
|
|
}
|
|
|
|
// AppendArrayEnd adds markers to indicate the end of an array.
|
|
func (Encoder) AppendArrayEnd(dst []byte) []byte {
|
|
return append(dst, ']')
|
|
}
|
|
|
|
// AppendArrayDelim adds markers to indicate end of a particular array element.
|
|
func (Encoder) AppendArrayDelim(dst []byte) []byte {
|
|
if len(dst) > 0 {
|
|
return append(dst, ',')
|
|
}
|
|
return dst
|
|
}
|
|
|
|
// AppendBool converts the input bool to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendBool(dst []byte, val bool) []byte {
|
|
return strconv.AppendBool(dst, val)
|
|
}
|
|
|
|
// AppendBools encodes the input bools to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendBools(dst []byte, vals []bool) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendBool(dst, vals[0])
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendBool(append(dst, ','), val)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendInt converts the input int to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendInt(dst []byte, val int) []byte {
|
|
return strconv.AppendInt(dst, int64(val), 10)
|
|
}
|
|
|
|
// AppendInts encodes the input ints to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendInts(dst []byte, vals []int) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendInt8 converts the input []int8 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendInt8(dst []byte, val int8) []byte {
|
|
return strconv.AppendInt(dst, int64(val), 10)
|
|
}
|
|
|
|
// AppendInts8 encodes the input int8s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendInts8(dst []byte, vals []int8) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendInt16 converts the input int16 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendInt16(dst []byte, val int16) []byte {
|
|
return strconv.AppendInt(dst, int64(val), 10)
|
|
}
|
|
|
|
// AppendInts16 encodes the input int16s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendInts16(dst []byte, vals []int16) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendInt32 converts the input int32 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendInt32(dst []byte, val int32) []byte {
|
|
return strconv.AppendInt(dst, int64(val), 10)
|
|
}
|
|
|
|
// AppendInts32 encodes the input int32s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendInts32(dst []byte, vals []int32) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendInt64 converts the input int64 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendInt64(dst []byte, val int64) []byte {
|
|
return strconv.AppendInt(dst, val, 10)
|
|
}
|
|
|
|
// AppendInts64 encodes the input int64s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendInts64(dst []byte, vals []int64) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendInt(dst, vals[0], 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendInt(append(dst, ','), val, 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendUint converts the input uint to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendUint(dst []byte, val uint) []byte {
|
|
return strconv.AppendUint(dst, uint64(val), 10)
|
|
}
|
|
|
|
// AppendUints encodes the input uints to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendUints(dst []byte, vals []uint) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendUint8 converts the input uint8 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendUint8(dst []byte, val uint8) []byte {
|
|
return strconv.AppendUint(dst, uint64(val), 10)
|
|
}
|
|
|
|
// AppendUints8 encodes the input uint8s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendUint16 converts the input uint16 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendUint16(dst []byte, val uint16) []byte {
|
|
return strconv.AppendUint(dst, uint64(val), 10)
|
|
}
|
|
|
|
// AppendUints16 encodes the input uint16s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendUint32 converts the input uint32 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendUint32(dst []byte, val uint32) []byte {
|
|
return strconv.AppendUint(dst, uint64(val), 10)
|
|
}
|
|
|
|
// AppendUints32 encodes the input uint32s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendUint64 converts the input uint64 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
|
|
return strconv.AppendUint(dst, uint64(val), 10)
|
|
}
|
|
|
|
// AppendUints64 encodes the input uint64s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendUint(dst, vals[0], 10)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = strconv.AppendUint(append(dst, ','), val, 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
func appendFloat(dst []byte, val float64, bitSize int) []byte {
|
|
// JSON does not permit NaN or Infinity. A typical JSON encoder would fail
|
|
// with an error, but a logging library wants the data to get thru so we
|
|
// make a tradeoff and store those types as string.
|
|
switch {
|
|
case math.IsNaN(val):
|
|
return append(dst, `"NaN"`...)
|
|
case math.IsInf(val, 1):
|
|
return append(dst, `"+Inf"`...)
|
|
case math.IsInf(val, -1):
|
|
return append(dst, `"-Inf"`...)
|
|
}
|
|
return strconv.AppendFloat(dst, val, 'f', -1, bitSize)
|
|
}
|
|
|
|
// AppendFloat32 converts the input float32 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendFloat32(dst []byte, val float32) []byte {
|
|
return appendFloat(dst, float64(val), 32)
|
|
}
|
|
|
|
// AppendFloats32 encodes the input float32s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendFloats32(dst []byte, vals []float32) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = appendFloat(dst, float64(vals[0]), 32)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = appendFloat(append(dst, ','), float64(val), 32)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendFloat64 converts the input float64 to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (Encoder) AppendFloat64(dst []byte, val float64) []byte {
|
|
return appendFloat(dst, val, 64)
|
|
}
|
|
|
|
// AppendFloats64 encodes the input float64s to json and
|
|
// appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = appendFloat(dst, vals[0], 32)
|
|
if len(vals) > 1 {
|
|
for _, val := range vals[1:] {
|
|
dst = appendFloat(append(dst, ','), val, 64)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendInterface marshals the input interface to a string and
|
|
// appends the encoded string to the input byte slice.
|
|
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
|
|
marshaled, err := json.Marshal(i)
|
|
if err != nil {
|
|
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
|
|
}
|
|
return append(dst, marshaled...)
|
|
}
|
|
|
|
// AppendObjectData takes in an object that is already in a byte array
|
|
// and adds it to the dst.
|
|
func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
|
|
// Two conditions we want to put a ',' between existing content and
|
|
// new content:
|
|
// 1. new content starts with '{' - which shd be dropped OR
|
|
// 2. existing content has already other fields
|
|
if o[0] == '{' {
|
|
o[0] = ','
|
|
} else if len(dst) > 1 {
|
|
dst = append(dst, ',')
|
|
}
|
|
return append(dst, o...)
|
|
}
|
|
|
|
// AppendIPAddr adds IPv4 or IPv6 address to dst.
|
|
func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
|
|
return e.AppendString(dst, ip.String())
|
|
}
|
|
|
|
// AppendIPPrefix adds IPv4 or IPv6 Prefix (address & mask) to dst.
|
|
func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
|
|
return e.AppendString(dst, pfx.String())
|
|
|
|
}
|
|
|
|
// AppendMACAddr adds MAC address to dst.
|
|
func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
|
|
return e.AppendString(dst, ha.String())
|
|
}
|