-
Notifications
You must be signed in to change notification settings - Fork 1
/
logConfig.go
94 lines (87 loc) · 1.66 KB
/
logConfig.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
package logger
import (
"os"
"time"
)
type singleTime struct {
Hour int
Min int
Second int
TZone *time.Location
}
type SliceTime []singleTime
func NewSliceTime(hour, min, sec int, timeZone ...*time.Location) *SliceTime {
if len(timeZone) == 0 {
return &SliceTime{
{
Hour: hour,
Min: min,
Second: sec,
TZone: time.Local,
},
}
}
return &SliceTime{
{
Hour: hour,
Min: min,
Second: sec,
TZone: timeZone[0],
},
}
}
func (s *SliceTime) And(hour, min, sec int, timeZone ...*time.Location) *SliceTime {
if len(timeZone) == 0 {
*s = append(*s, singleTime{
Hour: hour,
Min: min,
Second: sec,
TZone: time.Local,
})
return s
}
*s = append(*s, singleTime{
Hour: hour,
Min: min,
Second: sec,
TZone: timeZone[0],
})
return s
}
type Config struct {
// 日志名称
Name string
// 日志插入首行描述
Desc string
// 过时日志存储路径
OldLogPath func(info os.FileInfo) string
// 日志最大存储时间(超时切片,优先生效)
MaxLogTime time.Duration
// 日志定时切片
SliceWhen SliceTime
}
func findNextByWhen(cfg Config) time.Duration {
if len(cfg.SliceWhen) != 0 {
now := time.Now()
minD := time.Hour * 24
for _, v := range cfg.SliceWhen {
t := time.Date(now.Year(), now.Month(), now.Day(), v.Hour, v.Min, v.Second, 0, v.TZone)
if t.After(now) {
minD = min(minD, t.Sub(now))
} else {
minD = min(minD, t.Add(time.Hour*24).Sub(now))
}
}
return minD
}
if cfg.MaxLogTime != 0 {
return cfg.MaxLogTime
}
return time.Hour * 24
}
func min(d time.Duration, sub time.Duration) time.Duration {
if d > sub {
return sub
}
return d
}