-
Notifications
You must be signed in to change notification settings - Fork 1
/
record.go
229 lines (214 loc) · 5.89 KB
/
record.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package logger
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"github.com/gildas/go-errors"
)
// Record is the map that contains all records of a log entry
//
// If the value at a key is a func() interface the func will be called when the record is marshaled
type Record map[string]interface{}
// NewRecord creates a new empty record
func NewRecord() Record {
return Record(make(map[string]interface{}))
}
// NewPooledRecord creates a new empty record
func NewPooledRecord() (record Record, release func()) {
record = Record(mapPool.Get())
return record, func() { record.Close() }
}
// Close returns the record to the pool
func (record Record) Close() {
mapPool.Put(record)
}
// Set sets the key and value if not yet set
func (record Record) Set(key string, value interface{}) Record {
if value == nil {
return record
}
if _, ok := record[key]; !ok {
record[key] = value
}
return record
}
// Merge merges a source Record into this Record
//
// values already set in this record cannot be overridden
func (record Record) Merge(source Record) Record {
for key, value := range source {
record.Set(key, value)
}
return record
}
// MarshalJSON marshals this into JSON
func (record Record) MarshalJSON() ([]byte, error) {
if len(record) == 0 {
return []byte("null"), nil
}
var (
buffer = bufferPool.Get()
comma = false
)
defer bufferPool.Put(buffer)
buffer.WriteString("{")
for key, raw := range record {
if raw == nil {
continue
}
if comma {
buffer.WriteString(",")
} else {
comma = true
}
buffer.WriteString(`"`)
buffer.WriteString(key)
buffer.WriteString(`":`)
jsonValue(raw, buffer)
}
buffer.WriteString("}")
return buffer.Bytes(), nil
}
// UnmarshalJSON unmarshals JSON into this
func (record *Record) UnmarshalJSON(payload []byte) error {
var placeholder map[string]interface{}
if err := json.Unmarshal(payload, &placeholder); err != nil {
return errors.JSONUnmarshalError.Wrap(err)
}
*record = Record(placeholder)
return nil
}
func jsonValue(object interface{}, buffer *bytes.Buffer) {
switch value := object.(type) {
case func() interface{}:
object = value()
case Redactable:
object = value.Redact()
}
// This looks ugly, but it goes way faster than reflection (that is used by json.Marshal)
if errorobject, ok := object.(error); ok {
payload, err := json.Marshal(errorobject)
if err != nil {
buffer.WriteString(`"`)
buffer.Write([]byte(errorobject.Error()))
buffer.WriteString(`"`)
}
buffer.Write(payload)
return
}
switch value := object.(type) {
case bool:
buffer.WriteString(strconv.FormatBool(value))
case *bool:
buffer.WriteString(strconv.FormatBool(*value))
case complex64:
buffer.WriteString(`"`)
buffer.WriteString(strconv.FormatComplex(complex128(value), 'g', -1, 64))
buffer.WriteString(`"`)
case *complex64:
buffer.WriteString(`"`)
buffer.WriteString(strconv.FormatComplex(complex128(*value), 'g', -1, 64))
buffer.WriteString(`"`)
case complex128:
buffer.WriteString(`"`)
buffer.WriteString(strconv.FormatComplex(value, 'g', -1, 128))
buffer.WriteString(`"`)
case *complex128:
buffer.WriteString(`"`)
buffer.WriteString(strconv.FormatComplex(*value, 'g', -1, 128))
buffer.WriteString(`"`)
case float32:
buffer.WriteString(strconv.FormatFloat(float64(value), 'g', -1, 32))
case *float32:
buffer.WriteString(strconv.FormatFloat(float64(*value), 'g', -1, 32))
case float64:
buffer.WriteString(strconv.FormatFloat(value, 'g', -1, 64))
case *float64:
buffer.WriteString(strconv.FormatFloat(*value, 'g', -1, 64))
case Level:
buffer.WriteString(strconv.FormatInt(int64(value), 10))
case int:
buffer.WriteString(strconv.FormatInt(int64(value), 10))
case *int:
buffer.WriteString(strconv.FormatInt(int64(*value), 10))
case int8:
buffer.WriteString(strconv.FormatInt(int64(value), 10))
case *int8:
buffer.WriteString(strconv.FormatInt(int64(*value), 10))
case int16:
buffer.WriteString(strconv.FormatInt(int64(value), 10))
case *int16:
buffer.WriteString(strconv.FormatInt(int64(*value), 10))
case int32:
buffer.WriteString(strconv.FormatInt(int64(value), 10))
case *int32:
buffer.WriteString(strconv.FormatInt(int64(*value), 10))
case int64:
buffer.WriteString(strconv.FormatInt(value, 10))
case *int64:
buffer.WriteString(strconv.FormatInt(*value, 10))
case string:
buffer.WriteString(`"`)
jsonEscape(value, buffer)
buffer.WriteString(`"`)
case *string:
buffer.WriteString(`"`)
jsonEscape(*value, buffer)
buffer.WriteString(`"`)
case uint:
buffer.WriteString(strconv.FormatUint(uint64(value), 10))
case *uint:
buffer.WriteString(strconv.FormatUint(uint64(*value), 10))
case uint8:
buffer.WriteString(strconv.FormatUint(uint64(value), 10))
case *uint8:
buffer.WriteString(strconv.FormatUint(uint64(*value), 10))
case uint16:
buffer.WriteString(strconv.FormatUint(uint64(value), 10))
case *uint16:
buffer.WriteString(strconv.FormatUint(uint64(*value), 10))
case uint32:
buffer.WriteString(strconv.FormatUint(uint64(value), 10))
case *uint32:
buffer.WriteString(strconv.FormatUint(uint64(*value), 10))
case uint64:
buffer.WriteString(strconv.FormatUint(value, 10))
case *uint64:
buffer.WriteString(strconv.FormatUint(*value, 10))
default:
if payload, err := json.Marshal(object); err == nil {
buffer.Write(payload)
} else {
buffer.WriteString(`"`)
if stringer, ok := object.(fmt.Stringer); ok {
buffer.WriteString(stringer.String())
} else {
buffer.WriteString(fmt.Sprintf("%+#v", object))
}
buffer.WriteString(`"`)
}
}
}
func jsonEscape(value string, buffer *bytes.Buffer) {
for _, char := range value {
switch char {
case '\\':
buffer.WriteString(`\\`)
case '\b':
buffer.WriteString(`\b`)
case '\f':
buffer.WriteString(`\f`)
case '\n':
buffer.WriteString(`\n`)
case '\r':
buffer.WriteString(`\r`)
case '\t':
buffer.WriteString(`\t`)
case '"':
buffer.WriteString(`\"`)
default:
buffer.WriteRune(char)
}
}
}