-
Notifications
You must be signed in to change notification settings - Fork 3
/
write_closer_lumberjackv3.go
91 lines (78 loc) · 2.36 KB
/
write_closer_lumberjackv3.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
package ioKit
import (
"github.com/natefinch/lumberjack/v3"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"github.com/richelieu-yang/chimera/v3/src/core/mathKit"
"github.com/richelieu-yang/chimera/v3/src/file/fileKit"
"time"
)
type (
lumberjackOptions struct {
maxAge time.Duration
maxBackups int
localTime bool
compress bool
}
LumberJackOption func(opts *lumberjackOptions)
)
func WithMaxAge(maxAge time.Duration) LumberJackOption {
return func(opts *lumberjackOptions) {
opts.maxAge = maxAge
}
}
func WithMaxBackups(maxBackups int) LumberJackOption {
return func(opts *lumberjackOptions) {
opts.maxBackups = maxBackups
}
}
func WithLocalTime(localTime bool) LumberJackOption {
return func(opts *lumberjackOptions) {
opts.localTime = localTime
}
}
func WithCompress(compress bool) LumberJackOption {
return func(opts *lumberjackOptions) {
opts.compress = compress
}
}
func loadOptions(options ...LumberJackOption) *lumberjackOptions {
opts := &lumberjackOptions{
maxAge: 0,
maxBackups: 0,
localTime: true,
compress: false,
}
for _, option := range options {
option(opts)
}
opts.maxAge = mathKit.Max(opts.maxAge, 0)
opts.maxBackups = mathKit.Max(opts.maxBackups, 0)
return opts
}
// NewLumberJackWriteCloser 可rotate(依据传参maxSize)的io.WriteCloser.
/*
PS:
(1) 如果 当前目标文件 被人为删了,会丢失部分输出直至再次调用Rotate().
(2) 可以同时配置 MaxBackups 和 MaxAge.
@param maxSize unit: byte
@param options 可选配置:
WithCompress() [默认: 不压缩] 是否压缩被rotate的文件?
WithMaxBackups() [默认: 保留所有旧日志] 旧日志保存的最多数量
WithMaxAge() [默认: 保留所有旧日志] 旧日志保存的最长时间
WithLocalTime() [默认: true] true: 使用本地时间; false: 使用UTC时间
*/
func NewLumberJackWriteCloser(filePath string, maxSize int64, options ...LumberJackOption) (*lumberjack.Roller, error) {
if err := fileKit.AssertNotExistOrIsFile(filePath); err != nil {
return nil, err
}
opts := loadOptions(options...)
if opts.maxAge > 0 && opts.maxAge < time.Minute {
return nil, errorKit.Newf("maxAge(%s) is too small", opts.maxAge)
}
return lumberjack.NewRoller(filePath, maxSize, &lumberjack.Options{
MaxAge: opts.maxAge,
MaxBackups: opts.maxBackups,
LocalTime: opts.localTime,
Compress: opts.compress,
})
}