-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate.go
361 lines (327 loc) · 7.83 KB
/
rotate.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
package rotate
import (
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
)
var (
leftSymbol byte = '['
rightSymbol byte = ']'
perm os.FileMode = 0644
)
const (
_ = iota
KB = 1 << (10 * iota)
MB = 1 << (10 * iota)
GB = 1 << (10 * iota)
)
const (
Day = time.Hour * 24
Week = Day * 7
Month = Day * 30
)
// SetSymbol Set the left and right symbols of the file slice sequence number.
func SetSymbol(left, right byte) {
if left > 0 && right > 0 {
leftSymbol = left
rightSymbol = right
}
}
// SetPerm Set permissions for creating files and folders.
func SetPerm(permissions os.FileMode) {
perm = permissions
}
// ExpFunc Procedure for handler expired files.
type ExpFunc func([]string)
// Rotate The structure of the rotation file.
type Rotate struct {
dir string // storage folder path
layout string // generate filename format template
ext string // file extension
rotateDur time.Duration // rotation interval
loc *time.Location // time zone settings
maxAge time.Duration // maximum survival time
maxSize int64 // file size limit
expiredFunc ExpFunc // processing of expired files
deleteEmptyFileFlag bool // remove empty file flag
deleteEmptyDirFlag bool // remove empty dir flag
mutex *sync.RWMutex // lock
index int64 // rotated file index
writeSize int64 // the size of the file content that has been written
close chan struct{} // close goroutine channel
file *os.File // current file
tick *time.Timer // timing rotation timer
}
// New create Rotate, Requires directory to store logs, time formatted layout, file extension.
func New(dir, layout, ext string, options ...Option) (*Rotate, error) {
if len(dir) == 0 || len(layout) == 0 || len(ext) == 0 {
return nil, fmt.Errorf("dir, layout, ext name len equel zero")
}
if !strings.HasPrefix(dir, "/") {
if dir[1] != ':' && !strings.HasPrefix(dir, "./") {
dir = fmt.Sprintf("./%s", dir)
}
}
err := os.MkdirAll(dir, perm)
if err != nil {
return nil, err
}
if !strings.HasPrefix(ext, ".") {
ext = fmt.Sprintf(".%s", ext)
}
r := &Rotate{
// required parameters
dir: dir,
layout: layout,
ext: ext,
// optional parameters (defaults value)
rotateDur: Day,
loc: time.Local,
maxAge: 0,
maxSize: 0,
expiredFunc: nil,
deleteEmptyFileFlag: true,
deleteEmptyDirFlag: true,
// private parameters
mutex: new(sync.RWMutex),
index: 1,
writeSize: 0,
close: make(chan struct{}),
}
for _, option := range options {
option(r)
}
now := nowRotateTime(r.rotateDur, r.loc)
ok, _, index := r.getNowFilenameMax(now)
if ok {
index++
} else {
index = 0
}
file, err := r.open(r.genFilename(now, index))
if err != nil {
return nil, err
}
r.file = file
r.tick = time.NewTimer(calcDur(time.Now(), now.Add(r.rotateDur)))
go r.task()
r.expiredFileHandler()
return r, nil
}
// implement the io.Writer interface
func (r *Rotate) Write(data []byte) (int, error) {
var (
n int
err error
)
r.mutex.RLock()
if r.file == nil {
err = fmt.Errorf("file is close")
} else {
n, err = r.file.Write(data)
}
r.mutex.RUnlock()
if err != nil {
return n, err
}
r.mutex.Lock()
r.writeSize += int64(n)
r.mutex.Unlock()
if r.maxSize > 0 && r.writeSize >= r.maxSize {
r.rotateSize()
}
return n, err
}
// Sync method
func (r *Rotate) Sync() error {
return r.file.Sync()
}
// implement the io.Closer interface
func (r *Rotate) Close() error {
r.close <- struct{}{}
var f *os.File
r.mutex.Lock()
f = r.file
r.file = nil
r.mutex.Unlock()
_ = f.Sync()
return f.Close()
}
// goroutine tasks
func (r *Rotate) task() {
for {
select {
case <-r.close:
return
case now := <-r.tick.C:
r.tick.Reset(calcDur(now, nowRotateTime(r.rotateDur, r.loc).Add(r.rotateDur)))
r.rotateTime(now)
r.expiredFileHandler()
}
}
}
// rotateSize split by size
func (r *Rotate) rotateSize() {
i := r.index + 1
newFilename := r.genFilename(nowRotateTime(r.rotateDur, r.loc), i)
f, err := r.open(newFilename)
if err != nil {
panic(err)
}
var oldFile *os.File
r.mutex.Lock()
oldFile = r.file
r.file = f
r.writeSize = 0
r.index = i
r.mutex.Unlock()
if (i - 1) <= 1 {
newFilename = r.genFilename(nowRotateTime(r.rotateDur, r.loc), 1)
oldFilename := oldFile.Name()
_ = oldFile.Sync()
_ = oldFile.Close()
_ = os.Rename(oldFilename, newFilename)
} else {
_ = oldFile.Sync()
_ = oldFile.Close()
}
}
// rotateTime split by time
func (r *Rotate) rotateTime(now time.Time) {
filename := r.genFilename(now, 0)
f, err := r.open(filename)
if err != nil {
panic(err)
}
var oldFile *os.File
r.mutex.Lock()
oldFile = r.file
r.file = f
r.writeSize = 0
r.index = 1
r.mutex.Unlock()
if r.deleteEmptyFileFlag {
filename = oldFile.Name()
info, err := oldFile.Stat()
_ = oldFile.Sync()
_ = oldFile.Close()
if err != nil {
return
}
if info.Size() > 0 {
return
}
_ = os.Remove(filename)
} else {
_ = oldFile.Sync()
_ = oldFile.Close()
}
if r.deleteEmptyDirFlag {
emptyDirs := getEmptyDirs(r.dir)
for _, dir := range emptyDirs {
_ = os.Remove(dir)
}
}
}
// open file by file name
func (r *Rotate) open(filename string) (*os.File, error) {
dir, _ := path.Split(filename)
err := os.MkdirAll(dir, perm)
if err != nil {
return nil, err
}
return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, perm)
}
// genFilename generate file name
func (r *Rotate) genFilename(t time.Time, i int64) string {
var buf strings.Builder
buf.WriteString(join(r.dir, t.Format(r.layout)))
if i >= 1 {
buf.WriteByte(leftSymbol)
buf.WriteString(fmt.Sprintf("%d", i))
buf.WriteByte(rightSymbol)
}
buf.WriteString(r.ext)
return buf.String()
}
// parseFilename To parse the file name, the suffix must be removed
func (r *Rotate) parseFilename(filename string) (bool, time.Time, int64) {
rightIndex := len(filename) - 1
leftIndex := 0
if filename[rightIndex] == rightSymbol {
leftIndex = rightIndex
for 0 <= leftIndex {
if filename[leftIndex] == leftSymbol {
break
}
leftIndex--
}
if leftIndex < 0 {
return false, time.Time{}, 0
}
i, err := strconv.ParseInt(filename[leftIndex+1:rightIndex], 10, 64)
if err != nil {
return false, time.Time{}, 0
}
t, err := time.ParseInLocation(join(r.dir, r.layout), filename[:leftIndex], r.loc)
if err != nil {
return false, time.Time{}, 0
}
return true, t, i
} else {
t, err := time.ParseInLocation(join(r.dir, r.layout), filename, r.loc)
if err != nil {
return false, time.Time{}, 0
}
return true, t, 0
}
}
// getNowFilenameMax get the max chunk at this time
func (r *Rotate) getNowFilenameMax(now time.Time) (bool, string, int64) {
var result string
var fileOk bool
var maxIndex int64 = 0
files := getDirAllFiles(r.dir)
if len(files) <= 0 {
return fileOk, result, 0
}
for i := 0; i < len(files); i++ {
ok, t, index := r.parseFilename(strings.TrimSuffix(files[i], r.ext))
if ok && now.Equal(t) && index >= maxIndex {
fileOk = true
maxIndex = index
result = files[i]
}
}
return fileOk, result, maxIndex
}
// handle expired files
func (r *Rotate) expiredFileHandler() {
if r.expiredFunc != nil && r.maxAge > 0 {
exp := nowRotateTime(r.rotateDur, r.loc).Add(-r.maxAge)
result := r.getExpirationFiles(exp)
// prevent panic
defer func() {
if err := recover(); err != nil {
fmt.Printf("expired handler panic:%v", err)
}
}()
r.expiredFunc(result)
}
}
// get expired files
func (r *Rotate) getExpirationFiles(exp time.Time) []string {
var result []string
files := getDirAllFiles(r.dir)
for i := 0; i < len(files); i++ {
ok, t, _ := r.parseFilename(strings.TrimSuffix(files[i], r.ext))
if ok && (exp.Equal(t) || exp.After(t)) {
result = append(result, files[i])
}
}
return result
}