-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotatelogs.go
136 lines (117 loc) · 2.56 KB
/
rotatelogs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package rotatelogs
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type RotateLogs struct {
options
file *os.File
fileWildcard string
logPath string
logLinkPath string
logPathFormat string
mutex *sync.Mutex
rotateCh <-chan time.Time
closeCh chan struct{}
}
func New(logPath string, opts ...Option) *RotateLogs {
ins := &RotateLogs{
logPath: logPath,
mutex: &sync.Mutex{},
closeCh: make(chan struct{}, 1),
}
ins.options = defaultOpts
for _, opt := range opts {
opt(&ins.options)
}
ins.logLinkPath = ins.logPath
ins.logPathFormat = strings.Join([]string{ins.logPath, ins.timePeriod.TimeFormat()}, ".")
ins.fileWildcard = fmt.Sprintf("%s.*", ins.logPath)
return ins
}
func (slf *RotateLogs) Write(bytes []byte) (int, error) {
slf.mutex.Lock()
defer slf.mutex.Unlock()
if slf.file == nil {
if err := os.Mkdir(filepath.Dir(slf.logPath), 0755); err != nil && !os.IsExist(err) {
return 0, err
}
if err := slf.rotate(time.Now().Local(), true); err != nil {
return 0, err
}
if slf.timePeriod.isValid() {
go slf.listen()
}
}
n, err := slf.file.Write(bytes)
return n, err
}
func (slf *RotateLogs) Close() error {
slf.closeCh <- struct{}{}
return slf.file.Close()
}
func (slf *RotateLogs) listen() {
for {
select {
case <-slf.closeCh:
return
case now := <-slf.rotateCh:
_ = slf.rotate(now)
}
}
}
func (slf *RotateLogs) rotate(now time.Time, first ...bool) error {
if slf.timePeriod.isValid() {
duration := slf.timePeriod.UntilNextTime(now)
slf.rotateCh = time.After(duration)
}
filePath := now.Format(slf.logPathFormat)
if len(first) < 0 {
slf.mutex.Lock()
defer slf.mutex.Unlock()
}
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
}
if slf.file != nil {
_ = slf.file.Close()
}
slf.file = file
if len(slf.logLinkPath) > 0 {
_ = os.Remove(slf.logLinkPath)
_ = os.Link(filePath, slf.logLinkPath)
}
if slf.maxAge > 0 {
go slf.expire(now)
}
return nil
}
func (slf *RotateLogs) expire(now time.Time) {
cutoffTime := now.Add(-slf.maxAge)
matches, err := filepath.Glob(slf.fileWildcard)
if err != nil {
return
}
toUnlink := make([]string, 0, len(matches))
for _, path := range matches {
stat, err := os.Stat(path)
if err != nil {
continue
}
if slf.maxAge > 0 && stat.ModTime().After(cutoffTime) {
continue
}
if stat.Name() == filepath.Base(slf.logPath) {
continue
}
toUnlink = append(toUnlink, path)
}
for _, path := range toUnlink {
_ = os.Remove(path)
}
}