-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
234 lines (200 loc) · 4.64 KB
/
format.go
File metadata and controls
234 lines (200 loc) · 4.64 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
package logged
import (
"fmt"
"time"
)
const (
//LevelKey is the key used for message levels.
LevelKey = "lvl"
// MessageKey is the key used for message descriptions.
MessageKey = "msg"
timeFormat = "2006-01-02T15:04:05-0700" // ISO8601 format
)
// Formatter represents a log message formatter.
type Formatter interface {
// Format formats a log message.
Format(msg string, lvl Level, ctx []interface{}) []byte
}
// FormatterFunc is a function formatter.
type FormatterFunc func(msg string, lvl Level, ctx []interface{}) []byte
// Format formats a log message.
func (f FormatterFunc) Format(msg string, lvl Level, ctx []interface{}) []byte {
return f(msg, lvl, ctx)
}
var jsonPool = newPool(512)
// JSONFormat formats a log line in json format.
func JSONFormat() Formatter {
return FormatterFunc(func(msg string, lvl Level, ctx []interface{}) []byte {
buf := jsonPool.Get()
// Append initial keys to the buffer
buf.WriteByte('{')
buf.WriteString(`"` + LevelKey + `":"` + lvl.String() + `",`)
buf.WriteString(`"` + MessageKey + `":`)
quoteString(buf, msg)
// Append ctx to the buffer
for i := 0; i < len(ctx); i += 2 {
buf.WriteByte(',')
k, ok := ctx[i].(string)
if !ok {
buf.WriteString(`"` + errorKey + `"`)
buf.WriteByte(':')
formatJSONValue(buf, ctx[i])
continue
}
buf.WriteString(`"` + k + `"`)
buf.WriteByte(':')
formatJSONValue(buf, ctx[i+1])
}
buf.WriteString("}\n")
jsonPool.Put(buf)
return buf.Bytes()
})
}
// formatJSONValue formats a value, adding it to the buffer.
func formatJSONValue(buf *buffer, value interface{}) {
if value == nil {
buf.WriteString("null")
return
}
switch v := value.(type) {
case time.Time:
buf.WriteByte('"')
buf.AppendTime(v, timeFormat)
buf.WriteByte('"')
case bool:
buf.AppendBool(v)
case float32:
buf.AppendFloat(float64(v), 'g', -1, 64)
case float64:
buf.AppendFloat(v, 'g', -1, 64)
case int:
buf.AppendInt(int64(v))
case int8:
buf.AppendInt(int64(v))
case int16:
buf.AppendInt(int64(v))
case int32:
buf.AppendInt(int64(v))
case int64:
buf.AppendInt(v)
case uint:
buf.AppendUint(uint64(v))
case uint8:
buf.AppendUint(uint64(v))
case uint16:
buf.AppendUint(uint64(v))
case uint32:
buf.AppendUint(uint64(v))
case uint64:
buf.AppendUint(v)
case string:
quoteString(buf, v)
default:
quoteString(buf, fmt.Sprintf("%+v", value))
}
}
var logfmtPool = newPool(512)
// LogfmtFormat formats a log line in logfmt format.
func LogfmtFormat() Formatter {
return FormatterFunc(func(msg string, lvl Level, ctx []interface{}) []byte {
buf := logfmtPool.Get()
// Append initial keys to the buffer
buf.WriteString(LevelKey + "=" + lvl.String() + " ")
buf.WriteString(MessageKey + "=")
logfmtQuoteString(buf, msg)
// Append ctx to the buffer
for i := 0; i < len(ctx); i += 2 {
buf.WriteByte(' ')
k, ok := ctx[i].(string)
if !ok {
buf.WriteString(errorKey)
buf.WriteByte('=')
formatLogfmtValue(buf, ctx[i])
continue
}
buf.WriteString(k)
buf.WriteByte('=')
formatLogfmtValue(buf, ctx[i+1])
}
buf.WriteByte('\n')
logfmtPool.Put(buf)
return buf.Bytes()
})
}
// formatLogfmtValue formats a value, adding it to the buffer.
func formatLogfmtValue(buf *buffer, value interface{}) {
if value == nil {
return
}
switch v := value.(type) {
case time.Time:
buf.AppendTime(v, timeFormat)
case bool:
buf.AppendBool(v)
case float32:
buf.AppendFloat(float64(v), 'f', 3, 64)
case float64:
buf.AppendFloat(v, 'f', 3, 64)
case int:
buf.AppendInt(int64(v))
case int8:
buf.AppendInt(int64(v))
case int16:
buf.AppendInt(int64(v))
case int32:
buf.AppendInt(int64(v))
case int64:
buf.AppendInt(v)
case uint:
buf.AppendUint(uint64(v))
case uint8:
buf.AppendUint(uint64(v))
case uint16:
buf.AppendUint(uint64(v))
case uint32:
buf.AppendUint(uint64(v))
case uint64:
buf.AppendUint(v)
case string:
logfmtQuoteString(buf, v)
default:
logfmtQuoteString(buf, fmt.Sprintf("%+v", value))
}
}
func logfmtQuoteString(buf *buffer, s string) {
needsQuotes := false
for _, r := range s {
if r <= ' ' || r == '=' || r == '"' {
needsQuotes = true
}
}
if needsQuotes {
buf.WriteByte('"')
}
escapeString(buf, s)
if needsQuotes {
buf.WriteByte('"')
}
}
func quoteString(buf *buffer, s string) {
buf.WriteByte('"')
escapeString(buf, s)
buf.WriteByte('"')
}
func escapeString(buf *buffer, s string) {
for _, r := range s {
switch r {
case '\\', '"':
buf.WriteByte('\\')
buf.WriteByte(byte(r))
case '\n':
buf.WriteString("\\n")
case '\r':
buf.WriteString("\\r")
case '\t':
buf.WriteString("\\t")
default:
buf.WriteByte(byte(r))
}
}
}