zlog/fields.go

95 lines
2.2 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 {
2018-02-11 04:44:33 +00:00
dst = appendKey(dst, key)
2017-07-26 03:51:49 +00:00
switch val := fields[key].(type) {
case string:
2018-02-11 04:44:33 +00:00
dst = appendString(dst, val)
2017-07-26 03:51:49 +00:00
case []byte:
2018-02-11 04:44:33 +00:00
dst = appendBytes(dst, val)
2017-07-26 03:51:49 +00:00
case error:
2018-02-11 04:44:33 +00:00
dst = appendError(dst, val)
2017-07-26 03:51:49 +00:00
case []error:
2018-02-11 04:44:33 +00:00
dst = appendErrors(dst, val)
2017-07-26 03:51:49 +00:00
case bool:
2018-02-11 04:44:33 +00:00
dst = appendBool(dst, val)
2017-07-26 03:51:49 +00:00
case int:
2018-02-11 04:44:33 +00:00
dst = appendInt(dst, val)
2017-07-26 03:51:49 +00:00
case int8:
2018-02-11 04:44:33 +00:00
dst = appendInt8(dst, val)
2017-07-26 03:51:49 +00:00
case int16:
2018-02-11 04:44:33 +00:00
dst = appendInt16(dst, val)
2017-07-26 03:51:49 +00:00
case int32:
2018-02-11 04:44:33 +00:00
dst = appendInt32(dst, val)
2017-07-26 03:51:49 +00:00
case int64:
2018-02-11 04:44:33 +00:00
dst = appendInt64(dst, val)
2017-07-26 03:51:49 +00:00
case uint:
2018-02-11 04:44:33 +00:00
dst = appendUint(dst, val)
2017-07-26 03:51:49 +00:00
case uint8:
2018-02-11 04:44:33 +00:00
dst = appendUint8(dst, val)
2017-07-26 03:51:49 +00:00
case uint16:
2018-02-11 04:44:33 +00:00
dst = appendUint16(dst, val)
2017-07-26 03:51:49 +00:00
case uint32:
2018-02-11 04:44:33 +00:00
dst = appendUint32(dst, val)
2017-07-26 03:51:49 +00:00
case uint64:
2018-02-11 04:44:33 +00:00
dst = appendUint64(dst, val)
2017-07-26 03:51:49 +00:00
case float32:
2018-02-11 04:44:33 +00:00
dst = appendFloat32(dst, val)
2017-07-26 03:51:49 +00:00
case float64:
2018-02-11 04:44:33 +00:00
dst = appendFloat64(dst, val)
2017-07-26 03:51:49 +00:00
case time.Time:
2018-02-11 04:44:33 +00:00
dst = appendTime(dst, val, TimeFieldFormat)
2017-07-26 03:51:49 +00:00
case time.Duration:
2018-02-11 04:44:33 +00:00
dst = appendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
2017-07-26 03:51:49 +00:00
case []string:
2018-02-11 04:44:33 +00:00
dst = appendStrings(dst, val)
2017-07-26 03:51:49 +00:00
case []bool:
2018-02-11 04:44:33 +00:00
dst = appendBools(dst, val)
2017-07-26 03:51:49 +00:00
case []int:
2018-02-11 04:44:33 +00:00
dst = appendInts(dst, val)
2017-07-26 03:51:49 +00:00
case []int8:
2018-02-11 04:44:33 +00:00
dst = appendInts8(dst, val)
2017-07-26 03:51:49 +00:00
case []int16:
2018-02-11 04:44:33 +00:00
dst = appendInts16(dst, val)
2017-07-26 03:51:49 +00:00
case []int32:
2018-02-11 04:44:33 +00:00
dst = appendInts32(dst, val)
2017-07-26 03:51:49 +00:00
case []int64:
2018-02-11 04:44:33 +00:00
dst = appendInts64(dst, val)
2017-07-26 03:51:49 +00:00
case []uint:
2018-02-11 04:44:33 +00:00
dst = appendUints(dst, val)
2017-07-26 03:51:49 +00:00
// case []uint8:
// dst = appendUints8(dst, val)
case []uint16:
2018-02-11 04:44:33 +00:00
dst = appendUints16(dst, val)
2017-07-26 03:51:49 +00:00
case []uint32:
2018-02-11 04:44:33 +00:00
dst = appendUints32(dst, val)
2017-07-26 03:51:49 +00:00
case []uint64:
2018-02-11 04:44:33 +00:00
dst = appendUints64(dst, val)
2017-07-26 03:51:49 +00:00
case []float32:
2018-02-11 04:44:33 +00:00
dst = appendFloats32(dst, val)
2017-07-26 03:51:49 +00:00
case []float64:
2018-02-11 04:44:33 +00:00
dst = appendFloats64(dst, val)
2017-07-26 03:51:49 +00:00
case []time.Time:
2018-02-11 04:44:33 +00:00
dst = appendTimes(dst, val, TimeFieldFormat)
2017-07-26 03:51:49 +00:00
case []time.Duration:
2018-02-11 04:44:33 +00:00
dst = appendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
2017-07-26 03:51:49 +00:00
case nil:
dst = append(dst, "null"...)
default:
2018-02-11 04:44:33 +00:00
dst = appendInterface(dst, val)
2017-07-26 03:51:49 +00:00
}
}
return dst
}