-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_json.go
More file actions
183 lines (166 loc) · 4.12 KB
/
encoder_json.go
File metadata and controls
183 lines (166 loc) · 4.12 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
package blip
import (
"encoding/base64"
"encoding/json"
"time"
)
// JSONEncoder is an encoder that encodes log messages in JSON format.
type JSONEncoder struct {
TimeFormat string
TimePrecision time.Duration
Base64Encoding *base64.Encoding
KeyTime string
KeyLevel string
KeyMessage string
KeyStackTrace string
timeCache func(time.Time) string
}
var _ Encoder = (*JSONEncoder)(nil)
// NewJSONEncoder creates a new JSON encoder with the given configuration.
// The encoder formats log messages in JSON format, with optional and fields.
func NewJSONEncoder() *JSONEncoder {
return &JSONEncoder{
TimeFormat: defaultTimeFormat,
TimePrecision: defaultTimePrecision,
Base64Encoding: base64.StdEncoding,
KeyTime: "time",
KeyLevel: "level",
KeyMessage: "message",
KeyStackTrace: "stacktrace",
}
}
// Start writes the beginning of the log message.
func (e *JSONEncoder) Start(buf *Buffer) {
buf.WriteBytes('{')
}
// EncodeTime encodes the time of the log message.
func (e *JSONEncoder) EncodeTime(buf *Buffer) {
if e.TimeFormat == "" {
return
}
if e.TimePrecision > 0 {
if e.timeCache == nil {
e.timeCache = timeCache(e.TimeFormat, e.TimePrecision)
}
e.writeSafeField(buf, e.KeyTime, e.timeCache(timeNow()))
} else {
buf.WriteBytes('"')
buf.WriteString(e.KeyTime)
buf.WriteBytes('"', ':', '"')
buf.WriteTime(timeNow(), e.TimeFormat)
buf.WriteBytes('"')
}
}
// EncodeLevel encodes the log level of the message.
func (e *JSONEncoder) EncodeLevel(buf *Buffer, lev Level) {
if e.TimeFormat != "" {
buf.WriteBytes(',')
}
e.writeSafeField(buf, e.KeyLevel, e.levelString(lev))
}
// EncodeMessage encodes the log message.
func (e *JSONEncoder) EncodeMessage(buf *Buffer, msg string) {
buf.WriteBytes(',', '"')
buf.WriteString(e.KeyMessage)
buf.WriteBytes('"', ':')
buf.WriteEscapedString(msg)
}
// EncodeFields encodes the fields of the log message.
func (e *JSONEncoder) EncodeFields(buf *Buffer, _ Level, fields *[]Field) {
if fields == nil || len(*fields) == 0 {
return
}
for _, f := range *fields {
buf.WriteBytes(',')
buf.WriteEscapedString(f.Key)
buf.WriteBytes(':')
e.writeAny(buf, f.Value)
}
}
// EncodeStackTrace encodes the stack trace of the log message.
func (e *JSONEncoder) EncodeStackTrace(buf *Buffer, skip int) {
buf.WriteBytes(',', '"')
buf.WriteString(e.KeyStackTrace)
buf.WriteBytes('"', ':')
buf.WriteEscapedString(stackTrace(skip))
}
// End writes the end of the log message.
func (e *JSONEncoder) End(buf *Buffer) {
buf.WriteBytes('}', '\n')
}
// writeSafeField writes a field to the buffer not worrying about escaping it.
func (e *JSONEncoder) writeSafeField(buf *Buffer, key, val string) {
buf.WriteBytes('"')
buf.WriteString(key)
buf.WriteBytes('"', ':', '"')
buf.WriteString(val)
buf.WriteBytes('"')
}
// nolint:gocyclo
func (e *JSONEncoder) writeAny(buf *Buffer, val any) {
switch v := val.(type) {
case string:
buf.WriteEscapedString(v)
case []byte:
buf.WriteBase64(e.Base64Encoding, v)
case nil:
buf.WriteString("null")
case bool:
buf.WriteBool(v)
case int:
buf.WriteInt(int64(v))
case int8:
buf.WriteInt(int64(v))
case int16:
buf.WriteInt(int64(v))
case int32:
buf.WriteInt(int64(v))
case int64:
buf.WriteInt(v)
case uint:
buf.WriteUint(uint64(v))
case uint8:
buf.WriteUint(uint64(v))
case uint16:
buf.WriteUint(uint64(v))
case uint32:
buf.WriteUint(uint64(v))
case uint64:
buf.WriteUint(v)
case float32:
buf.WriteFloat(float64(v), 32)
case float64:
buf.WriteFloat(v, 64)
case time.Duration:
buf.WriteBytes('"')
buf.WriteDuration(v.Truncate(DurationFieldPrecision))
buf.WriteBytes('"')
case time.Time:
buf.WriteBytes('"')
buf.WriteTime(v, TimeFieldFormat)
buf.WriteBytes('"')
default:
//nolint:errchkjson
_ = json.NewEncoder(buf).Encode(v)
}
}
func (e *JSONEncoder) levelString(lev Level) string {
switch lev {
case LevelTrace:
return "trace"
case LevelDebug:
return "debug"
case LevelInfo:
return "info"
case LevelWarn:
return "warn"
case LevelError:
return "error"
case LevelPanic:
return "panic"
case LevelFatal:
return "fatal"
default:
panic("unreachable")
}
}