zlog/fields.go

127 lines
3.0 KiB
Go
Raw Normal View History

2017-07-26 03:51:49 +00:00
package zerolog
import (
"sort"
"time"
)
func appendFields(dst []byte, fields map[string]interface{}) []byte {
keys := make([]string, 0, len(fields))
for key := range fields {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
dst = appendKey(dst, key)
2017-07-26 03:51:49 +00:00
switch val := fields[key].(type) {
case string:
dst = appendString(dst, val)
2017-07-26 03:51:49 +00:00
case []byte:
dst = appendBytes(dst, val)
2017-07-26 03:51:49 +00:00
case error:
dst = appendError(dst, val)
2017-07-26 03:51:49 +00:00
case []error:
dst = appendErrors(dst, val)
2017-07-26 03:51:49 +00:00
case bool:
dst = appendBool(dst, val)
2017-07-26 03:51:49 +00:00
case int:
dst = appendInt(dst, val)
2017-07-26 03:51:49 +00:00
case int8:
dst = appendInt8(dst, val)
2017-07-26 03:51:49 +00:00
case int16:
dst = appendInt16(dst, val)
2017-07-26 03:51:49 +00:00
case int32:
dst = appendInt32(dst, val)
2017-07-26 03:51:49 +00:00
case int64:
dst = appendInt64(dst, val)
2017-07-26 03:51:49 +00:00
case uint:
dst = appendUint(dst, val)
2017-07-26 03:51:49 +00:00
case uint8:
dst = appendUint8(dst, val)
2017-07-26 03:51:49 +00:00
case uint16:
dst = appendUint16(dst, val)
2017-07-26 03:51:49 +00:00
case uint32:
dst = appendUint32(dst, val)
2017-07-26 03:51:49 +00:00
case uint64:
dst = appendUint64(dst, val)
2017-07-26 03:51:49 +00:00
case float32:
dst = appendFloat32(dst, val)
2017-07-26 03:51:49 +00:00
case float64:
dst = appendFloat64(dst, val)
2017-07-26 03:51:49 +00:00
case time.Time:
dst = appendTime(dst, val, TimeFieldFormat)
2017-07-26 03:51:49 +00:00
case time.Duration:
dst = appendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
case *string:
dst = appendString(dst, *val)
case *bool:
dst = appendBool(dst, *val)
case *int:
dst = appendInt(dst, *val)
case *int8:
dst = appendInt8(dst, *val)
case *int16:
dst = appendInt16(dst, *val)
case *int32:
dst = appendInt32(dst, *val)
case *int64:
dst = appendInt64(dst, *val)
case *uint:
dst = appendUint(dst, *val)
case *uint8:
dst = appendUint8(dst, *val)
case *uint16:
dst = appendUint16(dst, *val)
case *uint32:
dst = appendUint32(dst, *val)
case *uint64:
dst = appendUint64(dst, *val)
case *float32:
dst = appendFloat32(dst, *val)
case *float64:
dst = appendFloat64(dst, *val)
case *time.Time:
dst = appendTime(dst, *val, TimeFieldFormat)
case *time.Duration:
dst = appendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger)
2017-07-26 03:51:49 +00:00
case []string:
dst = appendStrings(dst, val)
2017-07-26 03:51:49 +00:00
case []bool:
dst = appendBools(dst, val)
2017-07-26 03:51:49 +00:00
case []int:
dst = appendInts(dst, val)
2017-07-26 03:51:49 +00:00
case []int8:
dst = appendInts8(dst, val)
2017-07-26 03:51:49 +00:00
case []int16:
dst = appendInts16(dst, val)
2017-07-26 03:51:49 +00:00
case []int32:
dst = appendInts32(dst, val)
2017-07-26 03:51:49 +00:00
case []int64:
dst = appendInts64(dst, val)
2017-07-26 03:51:49 +00:00
case []uint:
dst = appendUints(dst, val)
2017-07-26 03:51:49 +00:00
// case []uint8:
// dst = appendUints8(dst, val)
case []uint16:
dst = appendUints16(dst, val)
2017-07-26 03:51:49 +00:00
case []uint32:
dst = appendUints32(dst, val)
2017-07-26 03:51:49 +00:00
case []uint64:
dst = appendUints64(dst, val)
2017-07-26 03:51:49 +00:00
case []float32:
dst = appendFloats32(dst, val)
2017-07-26 03:51:49 +00:00
case []float64:
dst = appendFloats64(dst, val)
2017-07-26 03:51:49 +00:00
case []time.Time:
dst = appendTimes(dst, val, TimeFieldFormat)
2017-07-26 03:51:49 +00:00
case []time.Duration:
dst = appendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
2017-07-26 03:51:49 +00:00
case nil:
dst = appendNil(dst)
2017-07-26 03:51:49 +00:00
default:
dst = appendInterface(dst, val)
2017-07-26 03:51:49 +00:00
}
}
return dst
}