-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate.go
88 lines (77 loc) · 2.08 KB
/
rotate.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 zl
import (
"gopkg.in/natefinch/lumberjack.v2"
)
const (
FileNameDefault = "./log/app.jsonl"
MaxSizeDefault = 100 // megabytes
MaxBackupsDefault = 3
MaxAgeDefault = 7 // days
)
var (
fileName string
maxSize int
maxBackups int
maxAge int
localTime bool
compress bool
)
// newRotator
// See: https://github.com/natefinch/lumberjack
// See: https://github.com/uber-go/zap/blob/master/FAQ.md#does-zap-support-log-rotation
func newRotator() *lumberjack.Logger {
setRotateDefault()
res := &lumberjack.Logger{
Filename: fileName,
MaxSize: maxSize,
MaxBackups: maxBackups,
MaxAge: maxAge,
LocalTime: localTime,
Compress: compress,
}
return res
}
func setRotateDefault() {
if fileName == "" {
fileName = FileNameDefault
}
if maxSize == 0 {
maxSize = MaxSizeDefault
}
if maxBackups == 0 {
maxBackups = MaxBackupsDefault
}
if maxAge == 0 {
maxAge = MaxAgeDefault
}
}
// SetRotateFileName set the file to write logs to.
// See: https://github.com/natefinch/lumberjack#type-logger
func SetRotateFileName(val string) {
fileName = val
}
// SetRotateMaxSize set the maximum size in megabytes of the log file before it gets rotated.
// See: https://github.com/natefinch/lumberjack#type-logger
func SetRotateMaxSize(val int) {
maxSize = val
}
// SetRotateMaxAge set the maximum number of days to retain.
// See: https://github.com/natefinch/lumberjack#type-logger
func SetRotateMaxAge(val int) {
maxAge = val
}
// SetRotateMaxBackups set the maximum number of old log files to retain.
// See: https://github.com/natefinch/lumberjack#type-logger
func SetRotateMaxBackups(val int) {
maxBackups = val
}
// SetRotateLocalTime determines if the time used for formatting the timestamps in backup files is the computer's local time.
// See: https://github.com/natefinch/lumberjack#type-logger
func SetRotateLocalTime(val bool) {
localTime = val
}
// SetRotateCompress determines if the rotated log files should be compressed using gzip.
// See: https://github.com/natefinch/lumberjack#type-logger
func SetRotateCompress(val bool) {
compress = val
}