-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield_encoder.go
More file actions
391 lines (348 loc) · 10.4 KB
/
field_encoder.go
File metadata and controls
391 lines (348 loc) · 10.4 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Copyright 2025 The Go-Spring Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package log
import (
"encoding/json"
"io"
"strconv"
"unicode/utf8"
)
// Writer defines the interface for writing raw data.
type Writer interface {
io.Writer
io.ByteWriter
io.StringWriter
}
// Encoder defines the interface for structured logging encoders.
// Implementations control how log fields are serialized (e.g. JSON, text).
type Encoder interface {
AppendEncoderBegin()
AppendEncoderEnd()
AppendObjectBegin()
AppendObjectEnd()
AppendArrayBegin()
AppendArrayEnd()
AppendKey(key string)
AppendBool(v bool)
AppendInt64(v int64)
AppendUint64(v uint64)
AppendFloat64(v float64)
AppendString(v string)
AppendReflect(v any)
}
var (
_ Encoder = (*JSONEncoder)(nil)
_ Encoder = (*TextEncoder)(nil)
)
// JSONTokenType represents the type of the last token written to JSONEncoder.
// It is used to determine when separators (commas) are required.
type JSONTokenType int
const (
JSONTokenUnknown JSONTokenType = iota
JSONTokenObjectBegin
JSONTokenObjectEnd
JSONTokenArrayBegin
JSONTokenArrayEnd
JSONTokenKey
JSONTokenValue
)
// JSONEncoder encodes log fields into standard JSON format.
type JSONEncoder struct {
out Writer // Buffer to write JSON output.
last JSONTokenType // The last token type written.
}
// NewJSONEncoder creates a new JSONEncoder.
func NewJSONEncoder(out Writer) *JSONEncoder {
return &JSONEncoder{out: out, last: JSONTokenUnknown}
}
// Reset resets the encoder's state.
func (enc *JSONEncoder) Reset() {
enc.last = JSONTokenUnknown
}
// AppendEncoderBegin writes the start of an encoder section.
func (enc *JSONEncoder) AppendEncoderBegin() {
enc.AppendObjectBegin()
}
// AppendEncoderEnd writes the end of an encoder section.
func (enc *JSONEncoder) AppendEncoderEnd() {
enc.AppendObjectEnd()
}
// AppendObjectBegin writes the beginning of a JSON object.
func (enc *JSONEncoder) AppendObjectBegin() {
enc.appendSeparator()
enc.last = JSONTokenObjectBegin
_ = enc.out.WriteByte('{')
}
// AppendObjectEnd writes the end of a JSON object.
func (enc *JSONEncoder) AppendObjectEnd() {
enc.last = JSONTokenObjectEnd
_ = enc.out.WriteByte('}')
}
// AppendArrayBegin writes the beginning of a JSON array.
func (enc *JSONEncoder) AppendArrayBegin() {
enc.appendSeparator()
enc.last = JSONTokenArrayBegin
_ = enc.out.WriteByte('[')
}
// AppendArrayEnd writes the end of a JSON array.
func (enc *JSONEncoder) AppendArrayEnd() {
enc.last = JSONTokenArrayEnd
_ = enc.out.WriteByte(']')
}
// appendSeparator writes a comma if the previous token
// requires separation (e.g., between values).
func (enc *JSONEncoder) appendSeparator() {
if enc.last == JSONTokenObjectEnd || enc.last == JSONTokenArrayEnd || enc.last == JSONTokenValue {
_ = enc.out.WriteByte(',')
}
}
// AppendKey writes a JSON key.
func (enc *JSONEncoder) AppendKey(key string) {
enc.appendSeparator()
enc.last = JSONTokenKey
_ = enc.out.WriteByte('"')
WriteLogString(enc.out, key)
_ = enc.out.WriteByte('"')
_ = enc.out.WriteByte(':')
}
// AppendBool writes a boolean value.
func (enc *JSONEncoder) AppendBool(v bool) {
enc.appendSeparator()
enc.last = JSONTokenValue
_, _ = enc.out.WriteString(strconv.FormatBool(v))
}
// AppendInt64 writes an int64 value.
func (enc *JSONEncoder) AppendInt64(v int64) {
enc.appendSeparator()
enc.last = JSONTokenValue
_, _ = enc.out.WriteString(strconv.FormatInt(v, 10))
}
// AppendUint64 writes an uint64 value.
func (enc *JSONEncoder) AppendUint64(u uint64) {
enc.appendSeparator()
enc.last = JSONTokenValue
_, _ = enc.out.WriteString(strconv.FormatUint(u, 10))
}
// AppendFloat64 writes a float64 value.
func (enc *JSONEncoder) AppendFloat64(v float64) {
enc.appendSeparator()
enc.last = JSONTokenValue
_, _ = enc.out.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
}
// AppendString writes a string value with proper escaping.
func (enc *JSONEncoder) AppendString(v string) {
enc.appendSeparator()
enc.last = JSONTokenValue
_ = enc.out.WriteByte('"')
WriteLogString(enc.out, v)
_ = enc.out.WriteByte('"')
}
// AppendReflect marshals any Go value to JSON and writes it.
// If marshalling fails, the error message is written as a string.
func (enc *JSONEncoder) AppendReflect(v any) {
enc.appendSeparator()
enc.last = JSONTokenValue
b, err := json.Marshal(v)
if err != nil {
_ = enc.out.WriteByte('"')
WriteLogString(enc.out, err.Error())
_ = enc.out.WriteByte('"')
return
}
_, _ = enc.out.Write(b)
}
// TextEncoder encodes fields as "key=value" pairs separated by a delimiter.
// For nested objects and arrays, it delegates to the embedded JSONEncoder.
type TextEncoder struct {
out Writer // Buffer to write the encoded output
separator string // Separator used between top-level key-value pairs
jsonEncoder *JSONEncoder // Embedded JSON encoder for nested objects/arrays
jsonDepth int8 // Tracks depth of nested JSON structures
hasWritten bool // Tracks if the first key-value has been written
}
// NewTextEncoder creates a new TextEncoder, using the specified separator.
func NewTextEncoder(out Writer, separator string) *TextEncoder {
return &TextEncoder{
out: out,
separator: separator,
jsonEncoder: &JSONEncoder{out: out},
}
}
// AppendEncoderBegin writes the start of an encoder section.
func (enc *TextEncoder) AppendEncoderBegin() {}
// AppendEncoderEnd writes the end of an encoder section.
func (enc *TextEncoder) AppendEncoderEnd() {}
// AppendObjectBegin signals the start of a JSON object.
// Increments the depth and delegates to the JSON encoder.
func (enc *TextEncoder) AppendObjectBegin() {
enc.jsonDepth++
enc.jsonEncoder.AppendObjectBegin()
}
// AppendObjectEnd signals the end of a JSON object.
// Decrements the depth and resets the JSON encoder if back to top level.
func (enc *TextEncoder) AppendObjectEnd() {
enc.jsonDepth--
enc.jsonEncoder.AppendObjectEnd()
if enc.jsonDepth == 0 {
enc.jsonEncoder.Reset()
}
}
// AppendArrayBegin signals the start of a JSON array.
// Increments the depth and delegates to the JSON encoder.
func (enc *TextEncoder) AppendArrayBegin() {
enc.jsonDepth++
enc.jsonEncoder.AppendArrayBegin()
}
// AppendArrayEnd signals the end of a JSON array.
// Decrements the depth and resets the JSON encoder if back to top level.
func (enc *TextEncoder) AppendArrayEnd() {
enc.jsonDepth--
enc.jsonEncoder.AppendArrayEnd()
if enc.jsonDepth == 0 {
enc.jsonEncoder.Reset()
}
}
// AppendKey appends a key for a key-value pair.
// If inside a JSON structure, the key is handled by the JSON encoder.
// Otherwise, it's written directly with proper separator handling.
func (enc *TextEncoder) AppendKey(key string) {
if enc.jsonDepth > 0 {
enc.jsonEncoder.AppendKey(key)
return
}
if enc.hasWritten {
_, _ = enc.out.WriteString(enc.separator)
} else {
enc.hasWritten = true
}
WriteLogString(enc.out, key)
_ = enc.out.WriteByte('=')
}
// AppendBool appends a boolean value, using JSON encoder if nested.
func (enc *TextEncoder) AppendBool(v bool) {
if enc.jsonDepth > 0 {
enc.jsonEncoder.AppendBool(v)
return
}
_, _ = enc.out.WriteString(strconv.FormatBool(v))
}
// AppendInt64 appends an int64 value, using JSON encoder if nested.
func (enc *TextEncoder) AppendInt64(v int64) {
if enc.jsonDepth > 0 {
enc.jsonEncoder.AppendInt64(v)
return
}
_, _ = enc.out.WriteString(strconv.FormatInt(v, 10))
}
// AppendUint64 appends a uint64 value, using JSON encoder if nested.
func (enc *TextEncoder) AppendUint64(v uint64) {
if enc.jsonDepth > 0 {
enc.jsonEncoder.AppendUint64(v)
return
}
_, _ = enc.out.WriteString(strconv.FormatUint(v, 10))
}
// AppendFloat64 appends a float64 value, using JSON encoder if nested.
func (enc *TextEncoder) AppendFloat64(v float64) {
if enc.jsonDepth > 0 {
enc.jsonEncoder.AppendFloat64(v)
return
}
_, _ = enc.out.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
}
// AppendString appends a string value, using JSON encoder if nested.
func (enc *TextEncoder) AppendString(v string) {
if enc.jsonDepth > 0 {
enc.jsonEncoder.AppendString(v)
return
}
WriteLogString(enc.out, v)
}
// AppendReflect uses reflection to marshal any value as JSON.
// If nested, delegates to JSON encoder.
func (enc *TextEncoder) AppendReflect(v any) {
if enc.jsonDepth > 0 {
enc.jsonEncoder.AppendReflect(v)
return
}
b, err := json.Marshal(v)
if err != nil {
WriteLogString(enc.out, err.Error())
return
}
_, _ = enc.out.Write(b)
}
/************************************* string ********************************/
// WriteLogString escapes and writes a string according to JSON rules.
func WriteLogString(out Writer, s string) {
for i := 0; i < len(s); {
// Try to add a single-byte (ASCII) character directly
if tryAddRuneSelf(out, s[i]) {
i++
continue
}
// Decode multi-byte UTF-8 character
r, size := utf8.DecodeRuneInString(s[i:])
// Handle invalid UTF-8 encoding
if tryAddRuneError(out, r, size) {
i++
continue
}
// Valid multi-byte rune; add as is
_, _ = out.WriteString(s[i : i+size])
i += size
}
}
// tryAddRuneSelf handles ASCII characters and escapes control/quote characters.
func tryAddRuneSelf(out Writer, b byte) bool {
const _hex = "0123456789abcdef"
if b >= utf8.RuneSelf {
return false // not a single-byte rune
}
if 0x20 <= b && b != '\\' && b != '"' {
_ = out.WriteByte(b)
return true
}
// Handle escaping
switch b {
case '\\', '"':
_ = out.WriteByte('\\')
_ = out.WriteByte(b)
case '\n':
_ = out.WriteByte('\\')
_ = out.WriteByte('n')
case '\r':
_ = out.WriteByte('\\')
_ = out.WriteByte('r')
case '\t':
_ = out.WriteByte('\\')
_ = out.WriteByte('t')
default:
// Encode bytes < 0x20, except for the escape sequences above.
_, _ = out.WriteString(`\u00`)
_ = out.WriteByte(_hex[b>>4])
_ = out.WriteByte(_hex[b&0xF])
}
return true
}
// tryAddRuneError checks and escapes invalid UTF-8 runes.
func tryAddRuneError(out Writer, r rune, size int) bool {
if r == utf8.RuneError && size == 1 {
_, _ = out.WriteString(`\ufffd`)
return true
}
return false
}