-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformatter.go
274 lines (246 loc) · 6.66 KB
/
formatter.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
package golog
import (
"bytes"
"fmt"
"os"
)
var unknownFile = []byte("???")
var (
// DefaultFormatter is the default formatter
DefaultFormatter = ParseFormat("[%l %D %T %s] %m")
// TimedRotatingFormatter is a formatter for TimedRotatingFileWriter
TimedRotatingFormatter = ParseFormat("[%l %T %s] %m")
)
// A Formatter containing a sequence of FormatParts.
type Formatter struct {
formatParts []FormatPart
}
// ParseFormat parses a format string into a formatter.
func ParseFormat(format string) (formatter *Formatter) {
if format == "" {
return
}
formatter = &Formatter{}
formatter.findParts([]byte(format))
formatter.appendByte('\n')
return
}
/*
Format formats a record to a bytes.Buffer.
Supported format directives:
%%: %
%l: short name of the level
%T: time string (HH:MM:SS)
%D: date string (YYYY-mm-DD)
%s: source code string (filename:line)
%S: full source code string (/path/filename.go:line)
*/
func (f *Formatter) Format(r *Record, buf *bytes.Buffer) {
for _, part := range f.formatParts {
part.Format(r, buf)
}
}
func (f *Formatter) findParts(format []byte) {
length := len(format)
index := bytes.IndexByte(format, '%')
if index == -1 || index == length-1 {
if length == 0 {
return
}
if length == 1 {
f.appendByte(format[0])
} else {
f.appendBytes(format)
}
return
}
if index > 1 {
f.appendBytes(format[:index])
} else if index == 1 {
f.appendByte(format[0])
}
switch c := format[index+1]; c {
case '%':
f.appendByte('%')
case 'l':
f.formatParts = append(f.formatParts, &LevelFormatPart{})
case 'T':
f.formatParts = append(f.formatParts, &TimeFormatPart{})
case 'D':
f.formatParts = append(f.formatParts, &DateFormatPart{})
case 's':
f.formatParts = append(f.formatParts, &SourceFormatPart{})
case 'S':
f.formatParts = append(f.formatParts, &FullSourceFormatPart{})
case 'm':
f.formatParts = append(f.formatParts, &MessageFormatPart{})
default:
f.appendBytes([]byte{'%', c})
}
f.findParts(format[index+2:])
return
}
// FormatPart is an interface containing the Format() method.
type FormatPart interface {
Format(r *Record, buf *bytes.Buffer)
}
// ByteFormatPart is a FormatPart containing a byte.
type ByteFormatPart struct {
byte byte
}
// Format writes its byte to the buf.
func (p *ByteFormatPart) Format(r *Record, buf *bytes.Buffer) {
buf.WriteByte(p.byte)
}
// appendByte appends a byte to the formatter.
// If the previous FormatPart is a ByteFormatPart or BytesFormatPart, they will be merged into a BytesFormatPart;
// otherwise a new ByteFormatPart will be created.
func (f *Formatter) appendByte(b byte) {
parts := f.formatParts
count := len(parts)
if count == 0 {
f.formatParts = append(parts, &ByteFormatPart{byte: b})
} else {
var p FormatPart
lastPart := parts[count-1]
switch lp := lastPart.(type) {
case *ByteFormatPart:
p = &BytesFormatPart{
bytes: []byte{lp.byte, b},
}
case *BytesFormatPart:
p = &BytesFormatPart{
bytes: append(lp.bytes, b),
}
default:
p = &ByteFormatPart{byte: b}
f.formatParts = append(parts, p)
return
}
f.formatParts[count-1] = p
}
}
// BytesFormatPart is a FormatPart containing a byte slice.
type BytesFormatPart struct {
bytes []byte
}
// Format writes its bytes to the buf.
func (p *BytesFormatPart) Format(r *Record, buf *bytes.Buffer) {
buf.Write(p.bytes)
}
// appendBytes appends a byte slice to the formatter.
// If the previous FormatPart is a ByteFormatPart or BytesFormatPart, they will be merged into a BytesFormatPart;
// otherwise a new BytesFormatPart will be created.
func (f *Formatter) appendBytes(bs []byte) {
parts := f.formatParts
count := len(parts)
if count == 0 {
f.formatParts = append(parts, &BytesFormatPart{bytes: bs})
} else {
var p FormatPart
lastPart := parts[count-1]
switch lp := lastPart.(type) {
case *ByteFormatPart: // won't reach here
p = &BytesFormatPart{
bytes: append([]byte{lp.byte}, bs...),
}
case *BytesFormatPart:
p = &BytesFormatPart{
bytes: append(lp.bytes, bs...),
}
default:
p = &BytesFormatPart{bytes: bs}
f.formatParts = append(parts, p)
return
}
f.formatParts[count-1] = p
}
}
// LevelFormatPart is a FormatPart of the level placeholder.
type LevelFormatPart struct{}
// Format writes the short level name of the record to the buf.
func (p *LevelFormatPart) Format(r *Record, buf *bytes.Buffer) {
buf.WriteByte(levelNames[int(r.level)])
}
// TimeFormatPart is a FormatPart of the time placeholder.
type TimeFormatPart struct{}
// Format writes the time string of the record to the buf.
func (p *TimeFormatPart) Format(r *Record, buf *bytes.Buffer) {
if r.time == "" {
hour, min, sec := r.tm.Clock()
buf.Write(uint2Bytes2(hour))
buf.WriteByte(':')
buf.Write(uint2Bytes2(min))
buf.WriteByte(':')
buf.Write(uint2Bytes2(sec))
} else {
buf.WriteString(r.time)
}
}
// DateFormatPart is a FormatPart of the date placeholder.
type DateFormatPart struct{}
// Format writes the date string of the record to the buf.
func (p *DateFormatPart) Format(r *Record, buf *bytes.Buffer) {
if r.date == "" {
year, mon, day := r.tm.Date()
buf.Write(uint2Bytes4(year))
buf.WriteByte('-')
buf.Write(uint2Bytes2(int(mon)))
buf.WriteByte('-')
buf.Write(uint2Bytes2(day))
} else {
buf.WriteString(r.date)
}
}
// SourceFormatPart is a FormatPart of the source code placeholder.
type SourceFormatPart struct{}
// Format writes the source file name and line number of the record to the buf.
func (p *SourceFormatPart) Format(r *Record, buf *bytes.Buffer) {
if r.line > 0 {
length := len(r.file)
if length > 0 {
start := 0
end := length
for i := length - 1; i >= 0; i-- {
c := r.file[i]
if os.IsPathSeparator(c) {
start = i + 1
break
} else if c == '.' {
end = i
}
}
buf.WriteString(r.file[start:end])
buf.WriteByte(':')
buf.Write(fastUint2DynamicBytes(r.line))
return
}
}
buf.Write(unknownFile)
}
// FullSourceFormatPart is a FormatPart of the full source code placeholder.
type FullSourceFormatPart struct{}
// Format writes the source file path and line number of the record to the buf.
func (p *FullSourceFormatPart) Format(r *Record, buf *bytes.Buffer) {
if r.line > 0 {
buf.WriteString(r.file)
buf.WriteByte(':')
buf.Write(fastUint2DynamicBytes(r.line))
} else {
buf.Write(unknownFile)
}
}
// MessageFormatPart is a FormatPart of the message placeholder.
type MessageFormatPart struct{}
// Format writes the formatted message with args to the buf.
func (p *MessageFormatPart) Format(r *Record, buf *bytes.Buffer) {
if len(r.args) > 0 {
if r.message == "" {
fmt.Fprint(buf, r.args...)
} else {
fmt.Fprintf(buf, r.message, r.args...)
}
} else if r.message != "" {
buf.WriteString(r.message)
}
}