zlog/field.go

95 lines
2.6 KiB
Go
Raw Normal View History

2017-05-12 05:24:39 +00:00
package zerolog
import (
"encoding/json"
"fmt"
2017-05-12 05:24:39 +00:00
"strconv"
"time"
)
var now = time.Now
2017-05-12 05:24:39 +00:00
func appendKey(dst []byte, key string) []byte {
if len(dst) > 1 {
dst = append(dst, ',')
2017-05-12 05:24:39 +00:00
}
dst = appendJSONString(dst, key)
return append(dst, ':')
2017-05-12 05:24:39 +00:00
}
func appendString(dst []byte, key, val string) []byte {
return appendJSONString(appendKey(dst, key), val)
2017-05-12 05:24:39 +00:00
}
func appendError(dst []byte, err error) []byte {
return appendJSONString(appendKey(dst, ErrorFieldName), err.Error())
2017-05-12 05:24:39 +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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
func appendTime(dst []byte, key string, t time.Time) []byte {
return append(t.AppendFormat(append(appendKey(dst, key), '"'), TimeFieldFormat), '"')
2017-05-12 05:24:39 +00:00
}
func appendTimestamp(dst []byte) []byte {
return appendInt64(dst, TimestampFieldName, now().Unix())
2017-05-18 07:10:45 +00:00
}
2017-05-12 05:24:39 +00:00
func appendObject(dst []byte, key string, obj interface{}) []byte {
marshaled, err := json.Marshal(obj)
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
}