-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
88 lines (74 loc) · 1.84 KB
/
interface.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
package rotatelogs
import (
"fmt"
"time"
)
type IWriter interface {
Write(p []byte) (n int, err error)
Sync() error
Close() error
}
type Option interface {
Configure(*RotateLogs) error
}
type OptionFn func(*RotateLogs) error
func (o OptionFn) Configure(rl *RotateLogs) error {
return o(rl)
}
const (
MinRotationTime = 5 * time.Minute
MinAge = 1 * time.Hour
DefaultFileExt = ".log"
DoneTimeout = 15 * time.Second
)
// WithChannelLen 日志Channel长度,不设置则直接写入到文件
func WithChannelLen(channelLen int) Option {
return OptionFn(func(rl *RotateLogs) error {
if channelLen <= 0 {
return fmt.Errorf("channel length must be greater than zero")
}
rl.logChannelLen = channelLen
return nil
})
}
// WithFileExt 指定文件扩展名,默认.log
func WithFileExt(fileExt string) Option {
return OptionFn(func(rl *RotateLogs) error {
rl.fileExt = fileExt
return nil
})
}
// WithRotationTime 分割间隔时间,超过则创建新文件
func WithRotationTime(t time.Duration) Option {
return OptionFn(func(rl *RotateLogs) error {
if t < MinRotationTime {
return fmt.Errorf("rotationTime is too small")
}
rl.rotationTime = t
return nil
})
}
// WithRotateMaxSize 文件最大Byte,超过则创建新文件
func WithRotateMaxSize(maxSize int64) Option {
return OptionFn(func(rl *RotateLogs) error {
rl.rotateLogFileMaxSize = maxSize
return nil
})
}
// WithMaxAge 最大保留时长,超过则删除文件
func WithMaxAge(maxAge time.Duration) Option {
return OptionFn(func(rl *RotateLogs) error {
if maxAge < MinAge {
return fmt.Errorf("maxAge is too small")
}
rl.maxAge = maxAge
return nil
})
}
// WithMaxFileNum 最大保留文件数量,超过则删除文件
func WithMaxFileNum(maxNum int) Option {
return OptionFn(func(rl *RotateLogs) error {
rl.maxNum = maxNum
return nil
})
}