-
Notifications
You must be signed in to change notification settings - Fork 1
/
record.go
377 lines (310 loc) · 6.55 KB
/
record.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
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
package log
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type Recorder interface {
Record(record ...*Record)
Close()
}
type Tag map[string]string
func (t Tag) Add(key, val string) {
if t == nil {
return
}
t[key] = val
}
func (t Tag) Adds(kv ...string) {
for i := 0; i < len(kv)-1; i += 2 {
t.Add(kv[i], kv[i+1])
}
}
func (t Tag) Clone() Tag {
if t == nil {
return nil
}
var tag = make(Tag)
for k, v := range t {
tag[k] = v
}
return tag
}
type Record struct {
Tag Tag
Time time.Time
Level string
Message string
}
func (r *Record) Clone() *Record {
if r == nil {
return nil
}
var record = *r
record.Tag = record.Tag.Clone()
return &record
}
// Multi Recorder
type MultiRecorder struct {
records []Recorder
}
func NewMultiRecorder(recorder ...Recorder) *MultiRecorder {
return &MultiRecorder{
records: recorder,
}
}
func (m *MultiRecorder) Add(recorder ...Recorder) {
m.records = append(m.records, recorder...)
}
func (m *MultiRecorder) Record(record ...*Record) {
for _, r := range m.records {
r.Record(record...)
}
}
func (m *MultiRecorder) Close() {
for _, r := range m.records {
r.Close()
}
}
// RecordBuffer
type RecordBuffer struct {
off int
buf []*Record
rcd Recorder
}
func NewRecordBuffer(recorder Recorder) *RecordBuffer {
return NewRecordBufferSize(recorder, 128)
}
func NewRecordBufferSize(recorder Recorder, size int) *RecordBuffer {
r, ok := recorder.(*RecordBuffer)
if ok {
if len(r.buf) != size && size > 0 {
r.Flush()
r.buf = make([]*Record, size)
}
return r
}
if size <= 0 {
size = 128
}
return &RecordBuffer{
buf: make([]*Record, size),
rcd: recorder,
}
}
func (r *RecordBuffer) Record(record ...*Record) {
for len(record) > r.Available() {
var n int
if r.Buffered() == 0 {
r.rcd.Record(record...)
n = len(record)
} else {
n = copy(r.buf[r.off:], record)
r.off += n
r.Flush()
}
record = record[n:]
}
r.off += copy(r.buf[r.off:], record)
}
func (r *RecordBuffer) Reset(recorder Recorder) {
r.off = 0
r.rcd = recorder
}
func (r *RecordBuffer) Size() int {
return len(r.buf)
}
func (r *RecordBuffer) Buffered() int {
return r.off
}
func (r *RecordBuffer) Available() int {
return len(r.buf) - r.off
}
func (r *RecordBuffer) Flush() {
if r.off <= 0 {
return
}
r.rcd.Record(r.buf[:r.off]...)
r.off = 0
}
func (r *RecordBuffer) Recorder() Recorder {
return r.rcd
}
func (r *RecordBuffer) Close() {
r.Flush()
}
// StdioRecorder
type StdioRecorder struct {
formatter Formatter
closed bool
stdio *os.File
}
func NewStdoutRecorder(formatter Formatter) *StdioRecorder {
var s = new(StdioRecorder)
s.formatter = formatter
s.stdio = os.Stdout
return s
}
func NewStderrRecorder(formatter Formatter) *StdioRecorder {
var s = new(StdioRecorder)
s.formatter = formatter
s.stdio = os.Stderr
return s
}
func (s *StdioRecorder) Record(record ...*Record) {
if s.closed {
return
}
var builder = pool.Get().(*strings.Builder)
defer pool.Put(builder)
builder.Reset()
for _, r := range record {
_, _ = fmt.Fprintln(builder, s.formatter(r))
}
if len(record) > 0 {
_, _ = fmt.Fprint(s.stdio, builder.String())
}
}
func (s *StdioRecorder) Close() {
s.closed = true
return
}
// FileRecorder
type FileRecorder struct {
file *os.File
filename string
formatter Formatter
rotating FileRotating
}
type FileRotating interface {
Rotating(filename string, file *os.File) *os.File
}
func NewFileRecorder(filename string, formatter Formatter, rotating FileRotating) (*FileRecorder, error) {
_ = os.MkdirAll(filepath.Dir(filename), 0755)
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return nil, err
}
if formatter == nil {
formatter = FormatText
}
return &FileRecorder{
file: file,
filename: filename,
rotating: rotating,
formatter: formatter,
}, nil
}
func (f *FileRecorder) Record(record ...*Record) {
if f.file != nil && f.rotating != nil {
f.file = f.rotating.Rotating(f.filename, f.file)
}
if f.file == nil {
return
}
var builder = pool.Get().(*strings.Builder)
defer pool.Put(builder)
builder.Reset()
for _, r := range record {
_, _ = fmt.Fprintln(builder, f.formatter(r))
}
if len(record) > 0 {
_, _ = fmt.Fprint(f.file, builder.String())
}
}
func (f *FileRecorder) Close() {
if f.file == nil {
return
}
_ = f.file.Close()
f.file = nil
}
// FileCountRotating
type FileCountRotating struct {
maxBytes int
bakCount int
}
func NewFileCountRotating(maxBytes, bakCount int) *FileCountRotating {
return &FileCountRotating{
maxBytes: maxBytes,
bakCount: bakCount,
}
}
func (f *FileCountRotating) Rotating(filename string, file *os.File) *os.File {
// 文件备份数量为0, 则不备份
if f.bakCount == 0 {
_ = file.Close()
return nil
}
info, err := file.Stat()
if err != nil {
return file
}
// 不限制文件大小或不满足rotate条件, 则不rotate
if f.maxBytes <= 0 || int(info.Size()) < f.maxBytes {
return file
}
if err = file.Close(); err != nil {
return file
}
for i := f.bakCount - 2; i >= 0; i-- {
oldName := fmt.Sprintf("%s.%04d", filename, i)
if i == 0 {
oldName = filename
}
newName := fmt.Sprintf("%s.%04d", filename, i+1)
_ = os.Rename(oldName, newName)
}
empty, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return file
}
return empty
}
// FileTimeRotating
type FileTimeRotating struct {
align bool
interval time.Duration
}
func NewFileTimeRotating(interval time.Duration, align bool) *FileTimeRotating {
return &FileTimeRotating{
align: align,
interval: interval,
}
}
func (f *FileTimeRotating) Rotating(filename string, file *os.File) *os.File {
if f.interval == 0 {
_ = file.Close()
return nil
}
s, err := file.Stat()
if err != nil {
return file
}
info := fileInfo(s)
if info.CreateTime().Add(f.interval).Unix() > time.Now().Unix() {
return file
}
if err = file.Close(); err != nil {
return file
}
name := fmt.Sprintf("%s.%s", filename, info.CreateTime().Format("20060102150405"))
if f.align {
switch {
case f.interval >= time.Hour*24:
name = fmt.Sprintf("%s.%s", filename, info.CreateTime().Format("20060102000000"))
case f.interval >= time.Hour:
name = fmt.Sprintf("%s.%s", filename, info.CreateTime().Format("20060102150000"))
case f.interval >= time.Minute:
name = fmt.Sprintf("%s.%s", filename, info.CreateTime().Format("20060102150400"))
}
}
_ = os.Rename(filename, name)
empty, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return file
}
return empty
}