-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_file_output.go
56 lines (51 loc) · 1.11 KB
/
rotate_file_output.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
package logger
import (
"time"
"github.com/robfig/cron/v3"
"gopkg.in/natefinch/lumberjack.v2"
)
// cron
const (
Hourly = "@hourly"
Daily = "@daily"
)
// NewRotateFileOutput new rotate file output
func NewRotateFileOutput(
filename string,
maxSize int,
maxAge int,
maxBackups int,
localTime bool,
compress bool,
rotationCron string,
) *RotateFileOutput {
output := new(RotateFileOutput)
output.Logger = &lumberjack.Logger{
Filename: filename,
MaxSize: maxSize,
MaxAge: maxAge,
MaxBackups: maxBackups,
LocalTime: localTime,
Compress: compress,
}
if rotationCron != "" {
cronOptions := []cron.Option{cron.WithSeconds()}
if localTime {
cronOptions = append(cronOptions, cron.WithLocation(time.Local))
} else {
cronOptions = append(cronOptions, cron.WithLocation(time.UTC))
}
output.cron = cron.New(cronOptions...)
output.cron.AddFunc(rotationCron, func() {
output.Logger.Rotate()
})
output.cron.Start()
}
return output
}
// RotateFileOutput rotate file output
type RotateFileOutput struct {
*lumberjack.Logger
RotationTime time.Duration
cron *cron.Cron
}