Switch encoder using build tags

This commit is contained in:
Olivier Poitrey 2018-02-10 20:44:33 -08:00
parent 9a92fd2536
commit a6bc163a87
6 changed files with 460 additions and 146 deletions

View File

@ -3,8 +3,6 @@ package zerolog
import (
"sync"
"time"
"github.com/rs/zerolog/internal/json"
)
var arrayPool = &sync.Pool{
@ -53,109 +51,109 @@ func (a *Array) Object(obj LogObjectMarshaler) *Array {
// Str append the val as a string to the array.
func (a *Array) Str(val string) *Array {
a.buf = json.AppendString(append(a.buf, ','), val)
a.buf = appendString(append(a.buf, ','), val)
return a
}
// Bytes append the val as a string to the array.
func (a *Array) Bytes(val []byte) *Array {
a.buf = json.AppendBytes(append(a.buf, ','), val)
a.buf = appendBytes(append(a.buf, ','), val)
return a
}
// Err append the err as a string to the array.
func (a *Array) Err(err error) *Array {
a.buf = json.AppendError(append(a.buf, ','), err)
a.buf = appendError(append(a.buf, ','), err)
return a
}
// Bool append the val as a bool to the array.
func (a *Array) Bool(b bool) *Array {
a.buf = json.AppendBool(append(a.buf, ','), b)
a.buf = appendBool(append(a.buf, ','), b)
return a
}
// Int append i as a int to the array.
func (a *Array) Int(i int) *Array {
a.buf = json.AppendInt(append(a.buf, ','), i)
a.buf = appendInt(append(a.buf, ','), i)
return a
}
// Int8 append i as a int8 to the array.
func (a *Array) Int8(i int8) *Array {
a.buf = json.AppendInt8(append(a.buf, ','), i)
a.buf = appendInt8(append(a.buf, ','), i)
return a
}
// Int16 append i as a int16 to the array.
func (a *Array) Int16(i int16) *Array {
a.buf = json.AppendInt16(append(a.buf, ','), i)
a.buf = appendInt16(append(a.buf, ','), i)
return a
}
// Int32 append i as a int32 to the array.
func (a *Array) Int32(i int32) *Array {
a.buf = json.AppendInt32(append(a.buf, ','), i)
a.buf = appendInt32(append(a.buf, ','), i)
return a
}
// Int64 append i as a int64 to the array.
func (a *Array) Int64(i int64) *Array {
a.buf = json.AppendInt64(append(a.buf, ','), i)
a.buf = appendInt64(append(a.buf, ','), i)
return a
}
// Uint append i as a uint to the array.
func (a *Array) Uint(i uint) *Array {
a.buf = json.AppendUint(append(a.buf, ','), i)
a.buf = appendUint(append(a.buf, ','), i)
return a
}
// Uint8 append i as a uint8 to the array.
func (a *Array) Uint8(i uint8) *Array {
a.buf = json.AppendUint8(append(a.buf, ','), i)
a.buf = appendUint8(append(a.buf, ','), i)
return a
}
// Uint16 append i as a uint16 to the array.
func (a *Array) Uint16(i uint16) *Array {
a.buf = json.AppendUint16(append(a.buf, ','), i)
a.buf = appendUint16(append(a.buf, ','), i)
return a
}
// Uint32 append i as a uint32 to the array.
func (a *Array) Uint32(i uint32) *Array {
a.buf = json.AppendUint32(append(a.buf, ','), i)
a.buf = appendUint32(append(a.buf, ','), i)
return a
}
// Uint64 append i as a uint64 to the array.
func (a *Array) Uint64(i uint64) *Array {
a.buf = json.AppendUint64(append(a.buf, ','), i)
a.buf = appendUint64(append(a.buf, ','), i)
return a
}
// Float32 append f as a float32 to the array.
func (a *Array) Float32(f float32) *Array {
a.buf = json.AppendFloat32(append(a.buf, ','), f)
a.buf = appendFloat32(append(a.buf, ','), f)
return a
}
// Float64 append f as a float64 to the array.
func (a *Array) Float64(f float64) *Array {
a.buf = json.AppendFloat64(append(a.buf, ','), f)
a.buf = appendFloat64(append(a.buf, ','), f)
return a
}
// Time append t formated as string using zerolog.TimeFieldFormat.
func (a *Array) Time(t time.Time) *Array {
a.buf = json.AppendTime(append(a.buf, ','), t, TimeFieldFormat)
a.buf = appendTime(append(a.buf, ','), t, TimeFieldFormat)
return a
}
// Dur append d to the array.
func (a *Array) Dur(d time.Duration) *Array {
a.buf = json.AppendDuration(append(a.buf, ','), d, DurationFieldUnit, DurationFieldInteger)
a.buf = appendDuration(append(a.buf, ','), d, DurationFieldUnit, DurationFieldInteger)
return a
}
@ -164,6 +162,6 @@ func (a *Array) Interface(i interface{}) *Array {
if obj, ok := i.(LogObjectMarshaler); ok {
return a.Object(obj)
}
a.buf = json.AppendInterface(append(a.buf, ','), i)
a.buf = appendInterface(append(a.buf, ','), i)
return a
}

View File

@ -3,8 +3,6 @@ package zerolog
import (
"io/ioutil"
"time"
"github.com/rs/zerolog/internal/json"
)
// Context configures a new sub-logger with contextual fields.
@ -26,7 +24,7 @@ func (c Context) Fields(fields map[string]interface{}) Context {
// Dict adds the field key with the dict to the logger context.
func (c Context) Dict(key string, dict *Event) Context {
dict.buf = append(dict.buf, '}')
c.l.context = append(json.AppendKey(c.l.context, key), dict.buf...)
c.l.context = append(appendKey(c.l.context, key), dict.buf...)
eventPool.Put(dict)
return c
}
@ -35,7 +33,7 @@ func (c Context) Dict(key string, dict *Event) Context {
// Use zerolog.Arr() to create the array or pass a type that
// implement the LogArrayMarshaler interface.
func (c Context) Array(key string, arr LogArrayMarshaler) Context {
c.l.context = json.AppendKey(c.l.context, key)
c.l.context = appendKey(c.l.context, key)
if arr, ok := arr.(*Array); ok {
c.l.context = arr.write(c.l.context)
return c
@ -63,33 +61,33 @@ func (c Context) Object(key string, obj LogObjectMarshaler) Context {
// Str adds the field key with val as a string to the logger context.
func (c Context) Str(key, val string) Context {
c.l.context = json.AppendString(json.AppendKey(c.l.context, key), val)
c.l.context = appendString(appendKey(c.l.context, key), val)
return c
}
// Strs adds the field key with val as a string to the logger context.
func (c Context) Strs(key string, vals []string) Context {
c.l.context = json.AppendStrings(json.AppendKey(c.l.context, key), vals)
c.l.context = appendStrings(appendKey(c.l.context, key), vals)
return c
}
// Bytes adds the field key with val as a []byte to the logger context.
func (c Context) Bytes(key string, val []byte) Context {
c.l.context = json.AppendBytes(json.AppendKey(c.l.context, key), val)
c.l.context = appendBytes(appendKey(c.l.context, key), val)
return c
}
// AnErr adds the field key with err as a string to the logger context.
func (c Context) AnErr(key string, err error) Context {
if err != nil {
c.l.context = json.AppendError(json.AppendKey(c.l.context, key), err)
c.l.context = appendError(appendKey(c.l.context, key), err)
}
return c
}
// Errs adds the field key with errs as an array of strings to the logger context.
func (c Context) Errs(key string, errs []error) Context {
c.l.context = json.AppendErrors(json.AppendKey(c.l.context, key), errs)
c.l.context = appendErrors(appendKey(c.l.context, key), errs)
return c
}
@ -97,164 +95,164 @@ func (c Context) Errs(key string, errs []error) Context {
// To customize the key name, change zerolog.ErrorFieldName.
func (c Context) Err(err error) Context {
if err != nil {
c.l.context = json.AppendError(json.AppendKey(c.l.context, ErrorFieldName), err)
c.l.context = appendError(appendKey(c.l.context, ErrorFieldName), err)
}
return c
}
// Bool adds the field key with val as a bool to the logger context.
func (c Context) Bool(key string, b bool) Context {
c.l.context = json.AppendBool(json.AppendKey(c.l.context, key), b)
c.l.context = appendBool(appendKey(c.l.context, key), b)
return c
}
// Bools adds the field key with val as a []bool to the logger context.
func (c Context) Bools(key string, b []bool) Context {
c.l.context = json.AppendBools(json.AppendKey(c.l.context, key), b)
c.l.context = appendBools(appendKey(c.l.context, key), b)
return c
}
// Int adds the field key with i as a int to the logger context.
func (c Context) Int(key string, i int) Context {
c.l.context = json.AppendInt(json.AppendKey(c.l.context, key), i)
c.l.context = appendInt(appendKey(c.l.context, key), i)
return c
}
// Ints adds the field key with i as a []int to the logger context.
func (c Context) Ints(key string, i []int) Context {
c.l.context = json.AppendInts(json.AppendKey(c.l.context, key), i)
c.l.context = appendInts(appendKey(c.l.context, key), i)
return c
}
// Int8 adds the field key with i as a int8 to the logger context.
func (c Context) Int8(key string, i int8) Context {
c.l.context = json.AppendInt8(json.AppendKey(c.l.context, key), i)
c.l.context = appendInt8(appendKey(c.l.context, key), i)
return c
}
// Ints8 adds the field key with i as a []int8 to the logger context.
func (c Context) Ints8(key string, i []int8) Context {
c.l.context = json.AppendInts8(json.AppendKey(c.l.context, key), i)
c.l.context = appendInts8(appendKey(c.l.context, key), i)
return c
}
// Int16 adds the field key with i as a int16 to the logger context.
func (c Context) Int16(key string, i int16) Context {
c.l.context = json.AppendInt16(json.AppendKey(c.l.context, key), i)
c.l.context = appendInt16(appendKey(c.l.context, key), i)
return c
}
// Ints16 adds the field key with i as a []int16 to the logger context.
func (c Context) Ints16(key string, i []int16) Context {
c.l.context = json.AppendInts16(json.AppendKey(c.l.context, key), i)
c.l.context = appendInts16(appendKey(c.l.context, key), i)
return c
}
// Int32 adds the field key with i as a int32 to the logger context.
func (c Context) Int32(key string, i int32) Context {
c.l.context = json.AppendInt32(json.AppendKey(c.l.context, key), i)
c.l.context = appendInt32(appendKey(c.l.context, key), i)
return c
}
// Ints32 adds the field key with i as a []int32 to the logger context.
func (c Context) Ints32(key string, i []int32) Context {
c.l.context = json.AppendInts32(json.AppendKey(c.l.context, key), i)
c.l.context = appendInts32(appendKey(c.l.context, key), i)
return c
}
// Int64 adds the field key with i as a int64 to the logger context.
func (c Context) Int64(key string, i int64) Context {
c.l.context = json.AppendInt64(json.AppendKey(c.l.context, key), i)
c.l.context = appendInt64(appendKey(c.l.context, key), i)
return c
}
// Ints64 adds the field key with i as a []int64 to the logger context.
func (c Context) Ints64(key string, i []int64) Context {
c.l.context = json.AppendInts64(json.AppendKey(c.l.context, key), i)
c.l.context = appendInts64(appendKey(c.l.context, key), i)
return c
}
// Uint adds the field key with i as a uint to the logger context.
func (c Context) Uint(key string, i uint) Context {
c.l.context = json.AppendUint(json.AppendKey(c.l.context, key), i)
c.l.context = appendUint(appendKey(c.l.context, key), i)
return c
}
// Uints adds the field key with i as a []uint to the logger context.
func (c Context) Uints(key string, i []uint) Context {
c.l.context = json.AppendUints(json.AppendKey(c.l.context, key), i)
c.l.context = appendUints(appendKey(c.l.context, key), i)
return c
}
// Uint8 adds the field key with i as a uint8 to the logger context.
func (c Context) Uint8(key string, i uint8) Context {
c.l.context = json.AppendUint8(json.AppendKey(c.l.context, key), i)
c.l.context = appendUint8(appendKey(c.l.context, key), i)
return c
}
// Uints8 adds the field key with i as a []uint8 to the logger context.
func (c Context) Uints8(key string, i []uint8) Context {
c.l.context = json.AppendUints8(json.AppendKey(c.l.context, key), i)
c.l.context = appendUints8(appendKey(c.l.context, key), i)
return c
}
// Uint16 adds the field key with i as a uint16 to the logger context.
func (c Context) Uint16(key string, i uint16) Context {
c.l.context = json.AppendUint16(json.AppendKey(c.l.context, key), i)
c.l.context = appendUint16(appendKey(c.l.context, key), i)
return c
}
// Uints16 adds the field key with i as a []uint16 to the logger context.
func (c Context) Uints16(key string, i []uint16) Context {
c.l.context = json.AppendUints16(json.AppendKey(c.l.context, key), i)
c.l.context = appendUints16(appendKey(c.l.context, key), i)
return c
}
// Uint32 adds the field key with i as a uint32 to the logger context.
func (c Context) Uint32(key string, i uint32) Context {
c.l.context = json.AppendUint32(json.AppendKey(c.l.context, key), i)
c.l.context = appendUint32(appendKey(c.l.context, key), i)
return c
}
// Uints32 adds the field key with i as a []uint32 to the logger context.
func (c Context) Uints32(key string, i []uint32) Context {
c.l.context = json.AppendUints32(json.AppendKey(c.l.context, key), i)
c.l.context = appendUints32(appendKey(c.l.context, key), i)
return c
}
// Uint64 adds the field key with i as a uint64 to the logger context.
func (c Context) Uint64(key string, i uint64) Context {
c.l.context = json.AppendUint64(json.AppendKey(c.l.context, key), i)
c.l.context = appendUint64(appendKey(c.l.context, key), i)
return c
}
// Uints64 adds the field key with i as a []uint64 to the logger context.
func (c Context) Uints64(key string, i []uint64) Context {
c.l.context = json.AppendUints64(json.AppendKey(c.l.context, key), i)
c.l.context = appendUints64(appendKey(c.l.context, key), i)
return c
}
// Float32 adds the field key with f as a float32 to the logger context.
func (c Context) Float32(key string, f float32) Context {
c.l.context = json.AppendFloat32(json.AppendKey(c.l.context, key), f)
c.l.context = appendFloat32(appendKey(c.l.context, key), f)
return c
}
// Floats32 adds the field key with f as a []float32 to the logger context.
func (c Context) Floats32(key string, f []float32) Context {
c.l.context = json.AppendFloats32(json.AppendKey(c.l.context, key), f)
c.l.context = appendFloats32(appendKey(c.l.context, key), f)
return c
}
// Float64 adds the field key with f as a float64 to the logger context.
func (c Context) Float64(key string, f float64) Context {
c.l.context = json.AppendFloat64(json.AppendKey(c.l.context, key), f)
c.l.context = appendFloat64(appendKey(c.l.context, key), f)
return c
}
// Floats64 adds the field key with f as a []float64 to the logger context.
func (c Context) Floats64(key string, f []float64) Context {
c.l.context = json.AppendFloats64(json.AppendKey(c.l.context, key), f)
c.l.context = appendFloats64(appendKey(c.l.context, key), f)
return c
}
@ -275,31 +273,31 @@ func (c Context) Timestamp() Context {
// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (c Context) Time(key string, t time.Time) Context {
c.l.context = json.AppendTime(json.AppendKey(c.l.context, key), t, TimeFieldFormat)
c.l.context = appendTime(appendKey(c.l.context, key), t, TimeFieldFormat)
return c
}
// Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (c Context) Times(key string, t []time.Time) Context {
c.l.context = json.AppendTimes(json.AppendKey(c.l.context, key), t, TimeFieldFormat)
c.l.context = appendTimes(appendKey(c.l.context, key), t, TimeFieldFormat)
return c
}
// Dur adds the fields key with d divided by unit and stored as a float.
func (c Context) Dur(key string, d time.Duration) Context {
c.l.context = json.AppendDuration(json.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
c.l.context = appendDuration(appendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
return c
}
// Durs adds the fields key with d divided by unit and stored as a float.
func (c Context) Durs(key string, d []time.Duration) Context {
c.l.context = json.AppendDurations(json.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
c.l.context = appendDurations(appendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
return c
}
// Interface adds the field key with obj marshaled using reflection.
func (c Context) Interface(key string, i interface{}) Context {
c.l.context = json.AppendInterface(json.AppendKey(c.l.context, key), i)
c.l.context = appendInterface(appendKey(c.l.context, key), i)
return c
}

161
encoder.go Normal file
View File

@ -0,0 +1,161 @@
// +build !zerolog_binary
package zerolog
import (
"time"
"github.com/rs/zerolog/internal/json"
)
func appendKey(dst []byte, key string) []byte {
return json.AppendKey(dst, key)
}
func appendError(dst []byte, err error) []byte {
return json.AppendError(dst, err)
}
func appendErrors(dst []byte, errs []error) []byte {
return json.AppendErrors(dst, errs)
}
func appendStrings(dst []byte, vals []string) []byte {
return json.AppendStrings(dst, vals)
}
func appendString(dst []byte, s string) []byte {
return json.AppendString(dst, s)
}
func appendBytes(dst, b []byte) []byte {
return json.AppendBytes(dst, b)
}
func appendTime(dst []byte, t time.Time, format string) []byte {
return json.AppendTime(dst, t, format)
}
func appendTimes(dst []byte, vals []time.Time, format string) []byte {
return json.AppendTimes(dst, vals, format)
}
func appendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
return json.AppendDuration(dst, d, unit, useInt)
}
func appendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
return json.AppendDurations(dst, vals, unit, useInt)
}
func appendBool(dst []byte, val bool) []byte {
return json.AppendBool(dst, val)
}
func appendBools(dst []byte, vals []bool) []byte {
return json.AppendBools(dst, vals)
}
func appendInt(dst []byte, val int) []byte {
return json.AppendInt(dst, val)
}
func appendInts(dst []byte, vals []int) []byte {
return json.AppendInts(dst, vals)
}
func appendInt8(dst []byte, val int8) []byte {
return json.AppendInt8(dst, val)
}
func appendInts8(dst []byte, vals []int8) []byte {
return json.AppendInts8(dst, vals)
}
func appendInt16(dst []byte, val int16) []byte {
return json.AppendInt16(dst, val)
}
func appendInts16(dst []byte, vals []int16) []byte {
return json.AppendInts16(dst, vals)
}
func appendInt32(dst []byte, val int32) []byte {
return json.AppendInt32(dst, val)
}
func appendInts32(dst []byte, vals []int32) []byte {
return json.AppendInts32(dst, vals)
}
func appendInt64(dst []byte, val int64) []byte {
return json.AppendInt64(dst, val)
}
func appendInts64(dst []byte, vals []int64) []byte {
return json.AppendInts64(dst, vals)
}
func appendUint(dst []byte, val uint) []byte {
return json.AppendUint(dst, val)
}
func appendUints(dst []byte, vals []uint) []byte {
return json.AppendUints(dst, vals)
}
func appendUint8(dst []byte, val uint8) []byte {
return json.AppendUint8(dst, val)
}
func appendUints8(dst []byte, vals []uint8) []byte {
return json.AppendUints8(dst, vals)
}
func appendUint16(dst []byte, val uint16) []byte {
return json.AppendUint16(dst, val)
}
func appendUints16(dst []byte, vals []uint16) []byte {
return json.AppendUints16(dst, vals)
}
func appendUint32(dst []byte, val uint32) []byte {
return json.AppendUint32(dst, val)
}
func appendUints32(dst []byte, vals []uint32) []byte {
return json.AppendUints32(dst, vals)
}
func appendUint64(dst []byte, val uint64) []byte {
return json.AppendUint64(dst, val)
}
func appendUints64(dst []byte, vals []uint64) []byte {
return json.AppendUints64(dst, vals)
}
func appendFloat(dst []byte, val float64, bitSize int) []byte {
return json.AppendFloat(dst, val, bitSize)
}
func appendFloat32(dst []byte, val float32) []byte {
return json.AppendFloat32(dst, val)
}
func appendFloats32(dst []byte, vals []float32) []byte {
return json.AppendFloats32(dst, vals)
}
func appendFloat64(dst []byte, val float64) []byte {
return json.AppendFloat64(dst, val)
}
func appendFloats64(dst []byte, vals []float64) []byte {
return json.AppendFloats64(dst, vals)
}
func appendInterface(dst []byte, i interface{}) []byte {
return json.AppendInterface(dst, i)
}

161
encoder_binary.go Normal file
View File

@ -0,0 +1,161 @@
// +build zerolog_binary
package zerolog
import (
"time"
"github.com/rs/zerolog/internal/cbor"
)
func appendKey(dst []byte, key string) []byte {
return cbor.AppendKey(dst, key)
}
func appendError(dst []byte, err error) []byte {
return cbor.AppendError(dst, err)
}
func appendErrors(dst []byte, errs []error) []byte {
return cbor.AppendErrors(dst, errs)
}
func appendStrings(dst []byte, vals []string) []byte {
return cbor.AppendStrings(dst, vals)
}
func appendString(dst []byte, s string) []byte {
return cbor.AppendString(dst, s)
}
func appendBytes(dst, b []byte) []byte {
return cbor.AppendBytes(dst, b)
}
func appendTime(dst []byte, t time.Time, format string) []byte {
return cbor.AppendTime(dst, t, format)
}
func appendTimes(dst []byte, vals []time.Time, format string) []byte {
return cbor.AppendTimes(dst, vals, format)
}
func appendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
return cbor.AppendDuration(dst, d, unit, useInt)
}
func appendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
return cbor.AppendDurations(dst, vals, unit, useInt)
}
func appendBool(dst []byte, val bool) []byte {
return cbor.AppendBool(dst, val)
}
func appendBools(dst []byte, vals []bool) []byte {
return cbor.AppendBools(dst, vals)
}
func appendInt(dst []byte, val int) []byte {
return cbor.AppendInt(dst, val)
}
func appendInts(dst []byte, vals []int) []byte {
return cbor.AppendInts(dst, vals)
}
func appendInt8(dst []byte, val int8) []byte {
return cbor.AppendInt8(dst, val)
}
func appendInts8(dst []byte, vals []int8) []byte {
return cbor.AppendInts8(dst, vals)
}
func appendInt16(dst []byte, val int16) []byte {
return cbor.AppendInt16(dst, val)
}
func appendInts16(dst []byte, vals []int16) []byte {
return cbor.AppendInts16(dst, vals)
}
func appendInt32(dst []byte, val int32) []byte {
return cbor.AppendInt32(dst, val)
}
func appendInts32(dst []byte, vals []int32) []byte {
return cbor.AppendInts32(dst, vals)
}
func appendInt64(dst []byte, val int64) []byte {
return cbor.AppendInt64(dst, val)
}
func appendInts64(dst []byte, vals []int64) []byte {
return cbor.AppendInts64(dst, vals)
}
func appendUint(dst []byte, val uint) []byte {
return cbor.AppendUint(dst, val)
}
func appendUints(dst []byte, vals []uint) []byte {
return cbor.AppendUints(dst, vals)
}
func appendUint8(dst []byte, val uint8) []byte {
return cbor.AppendUint8(dst, val)
}
func appendUints8(dst []byte, vals []uint8) []byte {
return cbor.AppendUints8(dst, vals)
}
func appendUint16(dst []byte, val uint16) []byte {
return cbor.AppendUint16(dst, val)
}
func appendUints16(dst []byte, vals []uint16) []byte {
return cbor.AppendUints16(dst, vals)
}
func appendUint32(dst []byte, val uint32) []byte {
return cbor.AppendUint32(dst, val)
}
func appendUints32(dst []byte, vals []uint32) []byte {
return cbor.AppendUints32(dst, vals)
}
func appendUint64(dst []byte, val uint64) []byte {
return cbor.AppendUint64(dst, val)
}
func appendUints64(dst []byte, vals []uint64) []byte {
return cbor.AppendUints64(dst, vals)
}
func appendFloat(dst []byte, val float64, bitSize int) []byte {
return cbor.AppendFloat(dst, val, bitSize)
}
func appendFloat32(dst []byte, val float32) []byte {
return cbor.AppendFloat32(dst, val)
}
func appendFloats32(dst []byte, vals []float32) []byte {
return cbor.AppendFloats32(dst, vals)
}
func appendFloat64(dst []byte, val float64) []byte {
return cbor.AppendFloat64(dst, val)
}
func appendFloats64(dst []byte, vals []float64) []byte {
return cbor.AppendFloats64(dst, vals)
}
func appendInterface(dst []byte, i interface{}) []byte {
return cbor.AppendInterface(dst, i)
}

View File

@ -8,8 +8,6 @@ import (
"strconv"
"sync"
"time"
"github.com/rs/zerolog/internal/json"
)
var eventPool = &sync.Pool{
@ -97,7 +95,7 @@ func (e *Event) Msg(msg string) {
}
}
if msg != "" {
e.buf = json.AppendString(json.AppendKey(e.buf, MessageFieldName), msg)
e.buf = appendString(appendKey(e.buf, MessageFieldName), msg)
}
if e.done != nil {
defer e.done(msg)
@ -133,7 +131,7 @@ func (e *Event) Dict(key string, dict *Event) *Event {
if e == nil {
return e
}
e.buf = append(append(json.AppendKey(e.buf, key), dict.buf...), '}')
e.buf = append(append(appendKey(e.buf, key), dict.buf...), '}')
eventPool.Put(dict)
return e
}
@ -152,7 +150,7 @@ func (e *Event) Array(key string, arr LogArrayMarshaler) *Event {
if e == nil {
return e
}
e.buf = json.AppendKey(e.buf, key)
e.buf = appendKey(e.buf, key)
var a *Array
if aa, ok := arr.(*Array); ok {
a = aa
@ -183,7 +181,7 @@ func (e *Event) Object(key string, obj LogObjectMarshaler) *Event {
if e == nil {
return e
}
e.buf = json.AppendKey(e.buf, key)
e.buf = appendKey(e.buf, key)
e.appendObject(obj)
return e
}
@ -193,7 +191,7 @@ func (e *Event) Str(key, val string) *Event {
if e == nil {
return e
}
e.buf = json.AppendString(json.AppendKey(e.buf, key), val)
e.buf = appendString(appendKey(e.buf, key), val)
return e
}
@ -202,7 +200,7 @@ func (e *Event) Strs(key string, vals []string) *Event {
if e == nil {
return e
}
e.buf = json.AppendStrings(json.AppendKey(e.buf, key), vals)
e.buf = appendStrings(appendKey(e.buf, key), vals)
return e
}
@ -214,7 +212,7 @@ func (e *Event) Bytes(key string, val []byte) *Event {
if e == nil {
return e
}
e.buf = json.AppendBytes(json.AppendKey(e.buf, key), val)
e.buf = appendBytes(appendKey(e.buf, key), val)
return e
}
@ -225,7 +223,7 @@ func (e *Event) AnErr(key string, err error) *Event {
return e
}
if err != nil {
e.buf = json.AppendError(json.AppendKey(e.buf, key), err)
e.buf = appendError(appendKey(e.buf, key), err)
}
return e
}
@ -236,7 +234,7 @@ func (e *Event) Errs(key string, errs []error) *Event {
if e == nil {
return e
}
e.buf = json.AppendErrors(json.AppendKey(e.buf, key), errs)
e.buf = appendErrors(appendKey(e.buf, key), errs)
return e
}
@ -248,7 +246,7 @@ func (e *Event) Err(err error) *Event {
return e
}
if err != nil {
e.buf = json.AppendError(json.AppendKey(e.buf, ErrorFieldName), err)
e.buf = appendError(appendKey(e.buf, ErrorFieldName), err)
}
return e
}
@ -258,7 +256,7 @@ func (e *Event) Bool(key string, b bool) *Event {
if e == nil {
return e
}
e.buf = json.AppendBool(json.AppendKey(e.buf, key), b)
e.buf = appendBool(appendKey(e.buf, key), b)
return e
}
@ -267,7 +265,7 @@ func (e *Event) Bools(key string, b []bool) *Event {
if e == nil {
return e
}
e.buf = json.AppendBools(json.AppendKey(e.buf, key), b)
e.buf = appendBools(appendKey(e.buf, key), b)
return e
}
@ -276,7 +274,7 @@ func (e *Event) Int(key string, i int) *Event {
if e == nil {
return e
}
e.buf = json.AppendInt(json.AppendKey(e.buf, key), i)
e.buf = appendInt(appendKey(e.buf, key), i)
return e
}
@ -285,7 +283,7 @@ func (e *Event) Ints(key string, i []int) *Event {
if e == nil {
return e
}
e.buf = json.AppendInts(json.AppendKey(e.buf, key), i)
e.buf = appendInts(appendKey(e.buf, key), i)
return e
}
@ -294,7 +292,7 @@ func (e *Event) Int8(key string, i int8) *Event {
if e == nil {
return e
}
e.buf = json.AppendInt8(json.AppendKey(e.buf, key), i)
e.buf = appendInt8(appendKey(e.buf, key), i)
return e
}
@ -303,7 +301,7 @@ func (e *Event) Ints8(key string, i []int8) *Event {
if e == nil {
return e
}
e.buf = json.AppendInts8(json.AppendKey(e.buf, key), i)
e.buf = appendInts8(appendKey(e.buf, key), i)
return e
}
@ -312,7 +310,7 @@ func (e *Event) Int16(key string, i int16) *Event {
if e == nil {
return e
}
e.buf = json.AppendInt16(json.AppendKey(e.buf, key), i)
e.buf = appendInt16(appendKey(e.buf, key), i)
return e
}
@ -321,7 +319,7 @@ func (e *Event) Ints16(key string, i []int16) *Event {
if e == nil {
return e
}
e.buf = json.AppendInts16(json.AppendKey(e.buf, key), i)
e.buf = appendInts16(appendKey(e.buf, key), i)
return e
}
@ -330,7 +328,7 @@ func (e *Event) Int32(key string, i int32) *Event {
if e == nil {
return e
}
e.buf = json.AppendInt32(json.AppendKey(e.buf, key), i)
e.buf = appendInt32(appendKey(e.buf, key), i)
return e
}
@ -339,7 +337,7 @@ func (e *Event) Ints32(key string, i []int32) *Event {
if e == nil {
return e
}
e.buf = json.AppendInts32(json.AppendKey(e.buf, key), i)
e.buf = appendInts32(appendKey(e.buf, key), i)
return e
}
@ -348,7 +346,7 @@ func (e *Event) Int64(key string, i int64) *Event {
if e == nil {
return e
}
e.buf = json.AppendInt64(json.AppendKey(e.buf, key), i)
e.buf = appendInt64(appendKey(e.buf, key), i)
return e
}
@ -357,7 +355,7 @@ func (e *Event) Ints64(key string, i []int64) *Event {
if e == nil {
return e
}
e.buf = json.AppendInts64(json.AppendKey(e.buf, key), i)
e.buf = appendInts64(appendKey(e.buf, key), i)
return e
}
@ -366,7 +364,7 @@ func (e *Event) Uint(key string, i uint) *Event {
if e == nil {
return e
}
e.buf = json.AppendUint(json.AppendKey(e.buf, key), i)
e.buf = appendUint(appendKey(e.buf, key), i)
return e
}
@ -375,7 +373,7 @@ func (e *Event) Uints(key string, i []uint) *Event {
if e == nil {
return e
}
e.buf = json.AppendUints(json.AppendKey(e.buf, key), i)
e.buf = appendUints(appendKey(e.buf, key), i)
return e
}
@ -384,7 +382,7 @@ func (e *Event) Uint8(key string, i uint8) *Event {
if e == nil {
return e
}
e.buf = json.AppendUint8(json.AppendKey(e.buf, key), i)
e.buf = appendUint8(appendKey(e.buf, key), i)
return e
}
@ -393,7 +391,7 @@ func (e *Event) Uints8(key string, i []uint8) *Event {
if e == nil {
return e
}
e.buf = json.AppendUints8(json.AppendKey(e.buf, key), i)
e.buf = appendUints8(appendKey(e.buf, key), i)
return e
}
@ -402,7 +400,7 @@ func (e *Event) Uint16(key string, i uint16) *Event {
if e == nil {
return e
}
e.buf = json.AppendUint16(json.AppendKey(e.buf, key), i)
e.buf = appendUint16(appendKey(e.buf, key), i)
return e
}
@ -411,7 +409,7 @@ func (e *Event) Uints16(key string, i []uint16) *Event {
if e == nil {
return e
}
e.buf = json.AppendUints16(json.AppendKey(e.buf, key), i)
e.buf = appendUints16(appendKey(e.buf, key), i)
return e
}
@ -420,7 +418,7 @@ func (e *Event) Uint32(key string, i uint32) *Event {
if e == nil {
return e
}
e.buf = json.AppendUint32(json.AppendKey(e.buf, key), i)
e.buf = appendUint32(appendKey(e.buf, key), i)
return e
}
@ -429,7 +427,7 @@ func (e *Event) Uints32(key string, i []uint32) *Event {
if e == nil {
return e
}
e.buf = json.AppendUints32(json.AppendKey(e.buf, key), i)
e.buf = appendUints32(appendKey(e.buf, key), i)
return e
}
@ -438,7 +436,7 @@ func (e *Event) Uint64(key string, i uint64) *Event {
if e == nil {
return e
}
e.buf = json.AppendUint64(json.AppendKey(e.buf, key), i)
e.buf = appendUint64(appendKey(e.buf, key), i)
return e
}
@ -447,7 +445,7 @@ func (e *Event) Uints64(key string, i []uint64) *Event {
if e == nil {
return e
}
e.buf = json.AppendUints64(json.AppendKey(e.buf, key), i)
e.buf = appendUints64(appendKey(e.buf, key), i)
return e
}
@ -456,7 +454,7 @@ func (e *Event) Float32(key string, f float32) *Event {
if e == nil {
return e
}
e.buf = json.AppendFloat32(json.AppendKey(e.buf, key), f)
e.buf = appendFloat32(appendKey(e.buf, key), f)
return e
}
@ -465,7 +463,7 @@ func (e *Event) Floats32(key string, f []float32) *Event {
if e == nil {
return e
}
e.buf = json.AppendFloats32(json.AppendKey(e.buf, key), f)
e.buf = appendFloats32(appendKey(e.buf, key), f)
return e
}
@ -474,7 +472,7 @@ func (e *Event) Float64(key string, f float64) *Event {
if e == nil {
return e
}
e.buf = json.AppendFloat64(json.AppendKey(e.buf, key), f)
e.buf = appendFloat64(appendKey(e.buf, key), f)
return e
}
@ -483,7 +481,7 @@ func (e *Event) Floats64(key string, f []float64) *Event {
if e == nil {
return e
}
e.buf = json.AppendFloats64(json.AppendKey(e.buf, key), f)
e.buf = appendFloats64(appendKey(e.buf, key), f)
return e
}
@ -493,7 +491,7 @@ func (e *Event) Timestamp() *Event {
if e == nil {
return e
}
e.buf = json.AppendTime(json.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
e.buf = appendTime(appendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
return e
}
@ -502,7 +500,7 @@ func (e *Event) Time(key string, t time.Time) *Event {
if e == nil {
return e
}
e.buf = json.AppendTime(json.AppendKey(e.buf, key), t, TimeFieldFormat)
e.buf = appendTime(appendKey(e.buf, key), t, TimeFieldFormat)
return e
}
@ -511,7 +509,7 @@ func (e *Event) Times(key string, t []time.Time) *Event {
if e == nil {
return e
}
e.buf = json.AppendTimes(json.AppendKey(e.buf, key), t, TimeFieldFormat)
e.buf = appendTimes(appendKey(e.buf, key), t, TimeFieldFormat)
return e
}
@ -522,7 +520,7 @@ func (e *Event) Dur(key string, d time.Duration) *Event {
if e == nil {
return e
}
e.buf = json.AppendDuration(json.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
e.buf = appendDuration(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
return e
}
@ -533,7 +531,7 @@ func (e *Event) Durs(key string, d []time.Duration) *Event {
if e == nil {
return e
}
e.buf = json.AppendDurations(json.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
e.buf = appendDurations(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
return e
}
@ -548,7 +546,7 @@ func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event {
if t.After(start) {
d = t.Sub(start)
}
e.buf = json.AppendDuration(json.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
e.buf = appendDuration(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
return e
}
@ -560,7 +558,7 @@ func (e *Event) Interface(key string, i interface{}) *Event {
if obj, ok := i.(LogObjectMarshaler); ok {
return e.Object(key, obj)
}
e.buf = json.AppendInterface(json.AppendKey(e.buf, key), i)
e.buf = appendInterface(appendKey(e.buf, key), i)
return e
}
@ -577,6 +575,6 @@ func (e *Event) caller(skip int) *Event {
if !ok {
return e
}
e.buf = json.AppendString(json.AppendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line))
e.buf = appendString(appendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line))
return e
}

View File

@ -3,8 +3,6 @@ package zerolog
import (
"sort"
"time"
"github.com/rs/zerolog/internal/json"
)
func appendFields(dst []byte, fields map[string]interface{}) []byte {
@ -14,82 +12,82 @@ func appendFields(dst []byte, fields map[string]interface{}) []byte {
}
sort.Strings(keys)
for _, key := range keys {
dst = json.AppendKey(dst, key)
dst = appendKey(dst, key)
switch val := fields[key].(type) {
case string:
dst = json.AppendString(dst, val)
dst = appendString(dst, val)
case []byte:
dst = json.AppendBytes(dst, val)
dst = appendBytes(dst, val)
case error:
dst = json.AppendError(dst, val)
dst = appendError(dst, val)
case []error:
dst = json.AppendErrors(dst, val)
dst = appendErrors(dst, val)
case bool:
dst = json.AppendBool(dst, val)
dst = appendBool(dst, val)
case int:
dst = json.AppendInt(dst, val)
dst = appendInt(dst, val)
case int8:
dst = json.AppendInt8(dst, val)
dst = appendInt8(dst, val)
case int16:
dst = json.AppendInt16(dst, val)
dst = appendInt16(dst, val)
case int32:
dst = json.AppendInt32(dst, val)
dst = appendInt32(dst, val)
case int64:
dst = json.AppendInt64(dst, val)
dst = appendInt64(dst, val)
case uint:
dst = json.AppendUint(dst, val)
dst = appendUint(dst, val)
case uint8:
dst = json.AppendUint8(dst, val)
dst = appendUint8(dst, val)
case uint16:
dst = json.AppendUint16(dst, val)
dst = appendUint16(dst, val)
case uint32:
dst = json.AppendUint32(dst, val)
dst = appendUint32(dst, val)
case uint64:
dst = json.AppendUint64(dst, val)
dst = appendUint64(dst, val)
case float32:
dst = json.AppendFloat32(dst, val)
dst = appendFloat32(dst, val)
case float64:
dst = json.AppendFloat64(dst, val)
dst = appendFloat64(dst, val)
case time.Time:
dst = json.AppendTime(dst, val, TimeFieldFormat)
dst = appendTime(dst, val, TimeFieldFormat)
case time.Duration:
dst = json.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
dst = appendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
case []string:
dst = json.AppendStrings(dst, val)
dst = appendStrings(dst, val)
case []bool:
dst = json.AppendBools(dst, val)
dst = appendBools(dst, val)
case []int:
dst = json.AppendInts(dst, val)
dst = appendInts(dst, val)
case []int8:
dst = json.AppendInts8(dst, val)
dst = appendInts8(dst, val)
case []int16:
dst = json.AppendInts16(dst, val)
dst = appendInts16(dst, val)
case []int32:
dst = json.AppendInts32(dst, val)
dst = appendInts32(dst, val)
case []int64:
dst = json.AppendInts64(dst, val)
dst = appendInts64(dst, val)
case []uint:
dst = json.AppendUints(dst, val)
dst = appendUints(dst, val)
// case []uint8:
// dst = appendUints8(dst, val)
case []uint16:
dst = json.AppendUints16(dst, val)
dst = appendUints16(dst, val)
case []uint32:
dst = json.AppendUints32(dst, val)
dst = appendUints32(dst, val)
case []uint64:
dst = json.AppendUints64(dst, val)
dst = appendUints64(dst, val)
case []float32:
dst = json.AppendFloats32(dst, val)
dst = appendFloats32(dst, val)
case []float64:
dst = json.AppendFloats64(dst, val)
dst = appendFloats64(dst, val)
case []time.Time:
dst = json.AppendTimes(dst, val, TimeFieldFormat)
dst = appendTimes(dst, val, TimeFieldFormat)
case []time.Duration:
dst = json.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
dst = appendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
case nil:
dst = append(dst, "null"...)
default:
dst = json.AppendInterface(dst, val)
dst = appendInterface(dst, val)
}
}
return dst