-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg.go
243 lines (208 loc) · 5.43 KB
/
pkg.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package file_rotator
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// It writes messages by lines limit, file size limit, or time frequency.
type FileRotator struct {
sync.Mutex // write file order by order and atomic incr maxLinesCurLines and maxSizeCurSize
// The opened file
Filename string
fileWriter *os.File
// Rotate at line
MaxLines int
maxLinesCurLines int
// Rotate at size
MaxSize int
maxSizeCurSize int
// Rotate daily
Daily bool
MaxDays int64
dailyOpenDate int
Rotate bool
Perm os.FileMode
fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix
}
func NewFileRotator(filePath string) (*FileRotator, error) {
var err error
w := &FileRotator{
Filename: filepath.Clean(filePath),
MaxLines: 1000000,
MaxSize: 1 << 24, //16 MB
Daily: false,
MaxDays: 7,
Rotate: true,
Perm: 0660,
}
w.suffix = filepath.Ext(w.Filename)
w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix)
if w.suffix == "" {
w.suffix = ".log"
}
err = w.doRotate()
return w, err
}
// start file rotator. create file and set to locker-inside file writer.
func (w *FileRotator) startRotater() error {
file, err := w.createFile()
if err != nil {
return err
}
if w.fileWriter != nil {
w.fileWriter.Close()
}
w.fileWriter = file
return w.initFd()
}
func (w *FileRotator) needRotate(size int) bool {
var day int
if w.Daily {
_, _, day = time.Now().Date()
}
return (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) ||
(w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) ||
(w.Daily && day != w.dailyOpenDate)
}
// WriteMsg write bytes into file.
func (w *FileRotator) Write(b []byte) (n int, err error) {
if w.Rotate {
if w.needRotate(len(b)) {
w.Lock()
if w.needRotate(len(b)) {
if err := w.doRotate(); err != nil {
fmt.Fprintf(os.Stderr, "FileRotator.Write: rotate failed: %s, path: %s\n", err, w.Filename)
}
}
w.Unlock()
}
}
w.Lock()
n, err = w.fileWriter.Write(b)
if err == nil {
w.maxLinesCurLines++
w.maxSizeCurSize += len(b)
}
w.Unlock()
return
}
func (w *FileRotator) createFile() (*os.File, error) {
// Open the file
fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.Perm)
return fd, err
}
func (w *FileRotator) initFd() error {
fd := w.fileWriter
fInfo, err := fd.Stat()
if err != nil {
return fmt.Errorf("FileRotator.initFd: get stat err: %s\n", err)
}
w.maxSizeCurSize = int(fInfo.Size())
w.dailyOpenDate = time.Now().Day()
w.maxLinesCurLines = 0
if fInfo.Size() > 0 {
count, err := w.lines()
if err != nil {
return err
}
w.maxLinesCurLines = count
}
return nil
}
func (w *FileRotator) lines() (int, error) {
fd, err := os.Open(w.Filename)
if err != nil {
return 0, err
}
defer fd.Close()
buf := make([]byte, 32768) // 32k
count := 0
lineSep := []byte{'\n'}
for {
c, err := fd.Read(buf)
if err != nil && err != io.EOF {
return count, err
}
count += bytes.Count(buf[:c], lineSep)
if err == io.EOF {
break
}
}
return count, nil
}
// DoRotate means it need to write file in new file.
// new file name like xx.2013-01-01.log (daily) or xx.2013-01-01.001.log (by line or size)
func (w *FileRotator) doRotate() error {
var err error
now := time.Now()
// Find the next available number
num := 1
fName := ""
if w.MaxLines > 0 || w.MaxSize > 0 {
for ; err == nil && num <= 9999; num++ {
fName = w.fileNameOnly + fmt.Sprintf(".%s.%04d%s", now.Format("2006-01-02"), num, w.suffix)
_, err = os.Lstat(fName)
}
} else {
fName = fmt.Sprintf("%s.%s%s", w.fileNameOnly, now.Format("2006-01-02"), w.suffix)
_, err = os.Lstat(fName)
}
// return error if the last file checked still existed
if err == nil {
return fmt.Errorf("FileRotator.doRotate: cannot find free file name number to rename %s\n", w.Filename)
}
// close fileWriter before rename
if w.fileWriter != nil {
w.fileWriter.Close()
}
// Rename the file to its new found name
// even if occurs error,we MUST guarantee to restart new rotator
renameErr := os.Rename(w.Filename, fName)
// re-start rotator
startErr := w.startRotater()
go w.deleteOldFiles()
if startErr != nil {
return fmt.Errorf("FileRotator.doRotate: restart rotator failed: %s\n", startErr)
}
if renameErr != nil && !os.IsNotExist(err) {
return fmt.Errorf("FileRotator.doRotate: rename failed: %s\n", renameErr)
}
return nil
}
func (w *FileRotator) deleteOldFiles() {
dir := filepath.Dir(w.Filename)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
if path == w.Filename {
// We don't need to delete the w.Filename, because it is always up to date,
// and the w.Filename may not exsit because some race condition
return
}
if err != nil {
// Because some race condition, the file may not exsit now
fmt.Fprintf(os.Stderr, "FileRotator.deleteOldFiles: unable to get file info: %s, path: %s\n", err, path)
return
}
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.MaxDays) {
if strings.HasPrefix(path, w.fileNameOnly) &&
strings.HasSuffix(path, w.suffix) {
os.Remove(path)
}
}
return
})
}
// Destroy close the file description, close file writer.
func (w *FileRotator) Close() {
w.fileWriter.Close()
}
// Flush flush file.
// there are no buffering messages in file rotator in memory.
// flush file means sync file from disk.
func (w *FileRotator) Flush() {
w.fileWriter.Sync()
}