-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotateLog.go
47 lines (40 loc) · 1.24 KB
/
rotateLog.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
package xlog
import (
"strings"
"time"
"github.com/crx666/xlog/config"
"github.com/crx666/xlog/common"
"github.com/crx666/xlog/rotatelogs"
)
func GetRotateLogWriter(dir, file string, cfg *config.Rotatelog) LogFileWrite {
var ti time.Duration
if !strings.Contains(file, "$ti") && !strings.Contains(file, "$day") && !strings.Contains(file, "$hour") && !strings.Contains(file, "$minute") {
panic("split time log file need time format. please use $ti or $day or $hour or $minute of logName")
}
if !strings.Contains(file, common.LogFormal) {
file = file + common.LogTemp
}
if cfg.SplitDay > 0 { //按天切分
ti = time.Duration(cfg.SplitDay*24) * time.Hour
} else if cfg.SplitHour > 0 { //按小时切分
ti = time.Duration(cfg.SplitHour) * time.Hour
} else {
ti = time.Duration(cfg.SplitMinute) * time.Minute
}
options := []rotatelogs.Option{
rotatelogs.WithRotationCount(cfg.MaxSave), // 文件最大保存份数
rotatelogs.WithRotationTime(ti), // 日志切割时间间隔
}
if cfg.LinkName != "" {
options = append(options, rotatelogs.WithLinkName(cfg.LinkName)) // 生成软链,指向最新日志文件
}
hook, err := rotatelogs.New(
file,
dir,
options...,
)
if err != nil {
panic(err)
}
return hook
}