-
Notifications
You must be signed in to change notification settings - Fork 5
/
logrotate.go
136 lines (113 loc) · 2.63 KB
/
logrotate.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 logrotate
import (
"github.com/Me1onRind/logrotate/internal/ticker"
"os"
"path/filepath"
"sync"
"time"
)
type RotateLog struct {
file *os.File
logPath string
curLogLinkpath string
rotateTime time.Duration
maxAge time.Duration
deleteFileWildcard string
mutex *sync.Mutex
rotate <-chan time.Time // notify rotate event
close chan struct{} // close file and write goroutine
}
func NewRoteteLog(logPath string, opts ...Option) (*RotateLog, error) {
rl := &RotateLog{
mutex: &sync.Mutex{},
close: make(chan struct{}, 1),
logPath: logPath,
}
for _, opt := range opts {
opt(rl)
}
if err := os.Mkdir(filepath.Dir(rl.logPath), 0755); err != nil && !os.IsExist(err) {
return nil, err
}
if err := rl.rotateFile(time.Now()); err != nil {
return nil, err
}
if rl.rotateTime != 0 {
go rl.handleEvent()
}
return rl, nil
}
func (r *RotateLog) Write(b []byte) (int, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
//print(r.file.Name(), string(b))
n, err := r.file.Write(b)
return n, err
}
func (r *RotateLog) Close() error {
r.close <- struct{}{}
return r.file.Close()
}
func (r *RotateLog) handleEvent() {
for {
select {
case <-r.close:
return
case now := <-r.rotate:
r.rotateFile(now)
}
}
}
func (r *RotateLog) rotateFile(now time.Time) error {
if r.rotateTime != 0 {
nextRotateTime := ticker.CalRotateTimeDuration(now, r.rotateTime)
r.rotate = time.After(nextRotateTime)
}
latestLogPath := r.getLatestLogPath(now)
r.mutex.Lock()
defer r.mutex.Unlock()
file, err := os.OpenFile(latestLogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
}
if r.file != nil {
r.file.Close()
}
r.file = file
if len(r.curLogLinkpath) > 0 {
os.Remove(r.curLogLinkpath)
os.Link(latestLogPath, r.curLogLinkpath)
}
if r.maxAge > 0 && len(r.deleteFileWildcard) > 0 { // at present
go r.deleteExpiredFile(now)
}
return nil
}
// Judege expired by laste modify time
func (r *RotateLog) deleteExpiredFile(now time.Time) {
cutoffTime := now.Add(-r.maxAge)
matches, err := filepath.Glob(r.deleteFileWildcard)
if err != nil {
return
}
toUnlink := make([]string, 0, len(matches))
for _, path := range matches {
fileInfo, err := os.Stat(path)
if err != nil {
continue
}
if r.maxAge > 0 && fileInfo.ModTime().After(cutoffTime) {
continue
}
if len(r.curLogLinkpath) > 0 && fileInfo.Name() == filepath.Base(r.curLogLinkpath) {
continue
}
toUnlink = append(toUnlink, path)
}
for _, path := range toUnlink {
os.Remove(path)
}
}
func (r *RotateLog) getLatestLogPath(t time.Time) string {
return t.Format(r.logPath)
}