forked from rs/zerolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
138 lines (113 loc) · 2.94 KB
/
field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package zerolog
import (
"bytes"
"strconv"
"time"
)
var now = time.Now
type fieldMode uint8
const (
zeroFieldMode fieldMode = iota
rawFieldMode
quotedFieldMode
precomputedFieldMode
timestampFieldMode
)
// field define a logger field.
type field struct {
key string
mode fieldMode
val string
json []byte
}
func (f field) writeJSON(buf *bytes.Buffer) {
switch f.mode {
case zeroFieldMode:
return
case precomputedFieldMode:
buf.Write(f.json)
return
case timestampFieldMode:
writeJSONString(buf, TimestampFieldName)
buf.WriteByte(':')
buf.WriteString(strconv.FormatInt(now().Unix(), 10))
return
}
writeJSONString(buf, f.key)
buf.WriteByte(':')
switch f.mode {
case quotedFieldMode:
writeJSONString(buf, f.val)
case rawFieldMode:
buf.WriteString(f.val)
default:
panic("unknown field mode")
}
}
func (f field) compileJSON() field {
switch f.mode {
case zeroFieldMode, precomputedFieldMode, timestampFieldMode:
return f
}
buf := &bytes.Buffer{}
f.writeJSON(buf)
cf := field{
mode: precomputedFieldMode,
json: buf.Bytes(),
}
return cf
}
func fStr(key, val string) field {
return field{key, quotedFieldMode, val, nil}
}
func fErr(err error) field {
return field{ErrorFieldName, quotedFieldMode, err.Error(), nil}
}
func fBool(key string, b bool) field {
if b {
return field{key, rawFieldMode, "true", nil}
}
return field{key, rawFieldMode, "false", nil}
}
func fInt(key string, i int) field {
return field{key, rawFieldMode, strconv.FormatInt(int64(i), 10), nil}
}
func fInt8(key string, i int8) field {
return field{key, rawFieldMode, strconv.FormatInt(int64(i), 10), nil}
}
func fInt16(key string, i int16) field {
return field{key, rawFieldMode, strconv.FormatInt(int64(i), 10), nil}
}
func fInt32(key string, i int32) field {
return field{key, rawFieldMode, strconv.FormatInt(int64(i), 10), nil}
}
func fInt64(key string, i int64) field {
return field{key, rawFieldMode, strconv.FormatInt(i, 10), nil}
}
func fUint(key string, i uint) field {
return field{key, rawFieldMode, strconv.FormatUint(uint64(i), 10), nil}
}
func fUint8(key string, i uint8) field {
return field{key, rawFieldMode, strconv.FormatUint(uint64(i), 10), nil}
}
func fUint16(key string, i uint16) field {
return field{key, rawFieldMode, strconv.FormatUint(uint64(i), 10), nil}
}
func fUint32(key string, i uint32) field {
return field{key, rawFieldMode, strconv.FormatUint(uint64(i), 10), nil}
}
func fUint64(key string, i uint64) field {
return field{key, rawFieldMode, strconv.FormatUint(i, 10), nil}
}
func fFloat32(key string, f float32) field {
return field{key, rawFieldMode, strconv.FormatFloat(float64(f), 'f', -1, 32), nil}
}
func fFloat64(key string, f float64) field {
return field{key, rawFieldMode, strconv.FormatFloat(f, 'f', -1, 64), nil}
}
func fTimestamp() field {
return field{mode: timestampFieldMode}
}
func fTime(key string, t time.Time) field {
return field{key, quotedFieldMode, t.Format(TimeFieldFormat), nil}
}