2017-05-12 05:24:39 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
2017-05-20 02:43:59 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-05-12 05:24:39 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendKey(dst []byte, key string) []byte {
|
|
|
|
if len(dst) > 1 {
|
|
|
|
dst = append(dst, ',')
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
dst = appendJSONString(dst, key)
|
|
|
|
return append(dst, ':')
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendString(dst []byte, key, val string) []byte {
|
|
|
|
return appendJSONString(appendKey(dst, key), val)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 07:56:14 +00:00
|
|
|
func appendErrorKey(dst []byte, key string, err error) []byte {
|
|
|
|
if err == nil {
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
return appendJSONString(appendKey(dst, key), err.Error())
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendError(dst []byte, err error) []byte {
|
2017-06-02 07:56:14 +00:00
|
|
|
return appendErrorKey(dst, ErrorFieldName, err)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendBool(dst []byte, key string, val bool) []byte {
|
|
|
|
return strconv.AppendBool(appendKey(dst, key), val)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendInt(dst []byte, key string, val int) []byte {
|
|
|
|
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendInt8(dst []byte, key string, val int8) []byte {
|
|
|
|
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendInt16(dst []byte, key string, val int16) []byte {
|
|
|
|
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendInt32(dst []byte, key string, val int32) []byte {
|
|
|
|
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendInt64(dst []byte, key string, val int64) []byte {
|
|
|
|
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendUint(dst []byte, key string, val uint) []byte {
|
|
|
|
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendUint8(dst []byte, key string, val uint8) []byte {
|
|
|
|
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendUint16(dst []byte, key string, val uint16) []byte {
|
|
|
|
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendUint32(dst []byte, key string, val uint32) []byte {
|
|
|
|
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendUint64(dst []byte, key string, val uint64) []byte {
|
|
|
|
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendFloat32(dst []byte, key string, val float32) []byte {
|
|
|
|
return strconv.AppendFloat(appendKey(dst, key), float64(val), 'f', -1, 32)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendFloat64(dst []byte, key string, val float64) []byte {
|
|
|
|
return strconv.AppendFloat(appendKey(dst, key), float64(val), 'f', -1, 32)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendTime(dst []byte, key string, t time.Time) []byte {
|
2017-05-20 05:14:51 +00:00
|
|
|
if TimeFieldFormat == "" {
|
|
|
|
return appendInt64(dst, key, t.Unix())
|
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
return append(t.AppendFormat(append(appendKey(dst, key), '"'), TimeFieldFormat), '"')
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func appendTimestamp(dst []byte) []byte {
|
2017-05-20 09:22:57 +00:00
|
|
|
return appendTime(dst, TimestampFieldName, TimestampFunc())
|
2017-05-18 07:10:45 +00:00
|
|
|
}
|
2017-05-12 05:24:39 +00:00
|
|
|
|
2017-05-21 04:08:42 +00:00
|
|
|
func appendDuration(dst []byte, key string, d time.Duration) []byte {
|
|
|
|
if DurationFieldInteger {
|
|
|
|
return appendInt64(dst, key, int64(d/DurationFieldUnit))
|
2017-05-20 05:43:10 +00:00
|
|
|
}
|
2017-05-21 04:08:42 +00:00
|
|
|
return appendFloat64(dst, key, float64(d)/float64(DurationFieldUnit))
|
2017-05-20 05:43:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 05:25:37 +00:00
|
|
|
func appendInterface(dst []byte, key string, i interface{}) []byte {
|
|
|
|
marshaled, err := json.Marshal(i)
|
2017-05-20 02:43:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return appendString(dst, key, fmt.Sprintf("marshaling error: %v", err))
|
|
|
|
}
|
|
|
|
return append(appendKey(dst, key), marshaled...)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|