-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
220 lines (196 loc) · 5.38 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
package slog
import (
"bytes"
"fmt"
)
var unknownFile = []byte("???")
var (
// DefaultFormatter is the default formatter
DefaultFormatter = ParseFormat("%D %T %l %m")
// TimedRotatingFormatter is a formatter for TimedRotatingFileWriter
TimedRotatingFormatter = ParseFormat("[%l %T] %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 verbs:
%%: %
%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 '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)
}
}
// 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:
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)
}
}
// 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)])
}
var (
//StampNano = "15:04:05.000000000+07:00"
StampNano = "15:04:05.000000Z0700"
)
// 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) {
//hour, min, sec := r.time.Clock()
//buf.Write(uint2Bytes2(hour))
//buf.WriteByte(':')
//buf.Write(uint2Bytes2(min))
//buf.WriteByte(':')
//buf.Write(uint2Bytes2(sec))
buf.WriteString(r.time.Format(StampNano))
}
// 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) {
year, mon, day := r.time.Date()
buf.Write(uint2Bytes4(year))
buf.WriteByte('-')
buf.Write(uint2Bytes2(int(mon)))
buf.WriteByte('-')
buf.Write(uint2Bytes2(day))
}
// 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)
}
}