-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecord.go
294 lines (273 loc) · 7.37 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package logger
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/gildas/go-errors"
"github.com/google/uuid"
)
// 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 struct {
Data map[string]interface{}
KeysToRedact []string
}
// NewRecord creates a new empty record
func NewRecord() *Record {
return &Record{
Data: make(map[string]interface{}),
KeysToRedact: nil,
}
}
// Reset resets the record
func (record *Record) Reset() {
for key := range record.Data {
delete(record.Data, key)
}
record.KeysToRedact = nil
}
// NewPooledRecord creates a new empty record
func NewPooledRecord() (record *Record, release func()) {
record = mapPool.Get()
return record, func() { record.Close() }
}
// Close returns the record to the pool
func (record *Record) Close() {
mapPool.Put(record)
}
// Find gets the value at a key
func (record *Record) Find(key string) (value interface{}, found bool) {
if record == nil {
return nil, false
}
value, found = record.Data[key]
return
}
// Get gets the value at a key
func (record *Record) Get(key string) interface{} {
return record.Data[key]
}
// 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.Data[key]; !ok {
record.Data[key] = value
}
return record
}
// Delete deletes a key
func (record *Record) Delete(key string) *Record {
delete(record.Data, key)
return record
}
// AddKeysToRedact adds keys to redact
func (record *Record) AddKeysToRedact(keys ...string) *Record {
record.KeysToRedact = append(record.KeysToRedact, keys...)
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 {
if source == nil {
return record
}
for key, value := range source.Data {
record.Set(key, value)
}
record.KeysToRedact = append(record.KeysToRedact, source.KeysToRedact...)
return record
}
// MarshalJSON marshals this into JSON
func (record Record) MarshalJSON() ([]byte, error) {
if len(record.Data) == 0 {
return []byte("null"), nil
}
var (
buffer = bufferPool.Get()
comma = false
)
defer bufferPool.Put(buffer)
buffer.WriteString("{")
for key, raw := range record.Data {
showNils := strings.HasPrefix(key, "?")
key = strings.TrimPrefix(key, "?")
if !showNils {
if raw == nil {
continue
}
if value, ok := raw.(string); ok && value == "" {
continue
}
if id, ok := raw.(uuid.UUID); ok && id == uuid.Nil {
continue
}
if id, ok := raw.(interface{ IsNil() bool }); ok && id.IsNil() {
continue
}
}
if comma {
buffer.WriteString(",")
} else {
comma = true
}
buffer.WriteString(`"`)
buffer.WriteString(key)
buffer.WriteString(`":`)
jsonValue(raw, buffer, record.KeysToRedact...)
}
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{
Data: placeholder,
KeysToRedact: nil,
}
return nil
}
func jsonValue(object interface{}, buffer *bytes.Buffer, keyToRedact ...string) {
switch value := object.(type) {
case func() interface{}:
object = value()
case RedactableWithKeys:
object = value.Redact(keyToRedact...)
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)
}
}
}