-
Notifications
You must be signed in to change notification settings - Fork 79
/
logrotate.go
346 lines (311 loc) · 8.46 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
// Package lager is the package for lager
package log
import (
"archive/zip"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
var pathReplacer *strings.Replacer
// EscapPath escape path
func EscapPath(msg string) string {
return pathReplacer.Replace(msg)
}
func removeFile(path string) error {
fileInfo, err := os.Stat(path)
if err != nil {
return err
}
if fileInfo.IsDir() {
return nil
}
err = os.Remove(path)
if err != nil {
return err
}
return nil
}
func removeExceededFiles(path string, baseFileName string,
maxKeptCount int, rotateStage string) {
if maxKeptCount < 0 {
return
}
fileList := make([]string, 0, 2*maxKeptCount)
var pat string
if rotateStage == "rollover" {
//rotated file, svc.log.20060102150405000
pat = fmt.Sprintf(`%s\.[0-9]{1,17}$`, baseFileName)
} else if rotateStage == "backup" {
//backup compressed file, svc.log.20060102150405000.zip
pat = fmt.Sprintf(`%s\.[0-9]{17}\.zip$`, baseFileName)
} else {
return
}
fileList, err := FilterFileList(path, pat)
if err != nil {
Logger.Errorf(err, "filepath.Walk() path: %s failed.", EscapPath(path))
return
}
sort.Strings(fileList)
if len(fileList) <= maxKeptCount {
return
}
//remove exceeded files, keep file count below maxBackupCount
for len(fileList) > maxKeptCount {
filePath := fileList[0]
err := removeFile(filePath)
if err != nil {
Logger.Errorf(err, "remove filePath: %s failed.", EscapPath(filePath))
break
}
//remove the first element of a list
fileList = append(fileList[:0], fileList[1:]...)
}
}
//filePath: file full path, like ${_APP_LOG_DIR}/svc.log.1
//fileBaseName: rollover file base name, like svc.log
//replaceTimestamp: whether or not to replace the num. of a rolled file
func compressFile(filePath, fileBaseName string, replaceTimestamp bool) error {
ifp, err := os.Open(filePath)
if err != nil {
return err
}
defer ifp.Close()
var zipFilePath string
if replaceTimestamp {
//svc.log.1 -> svc.log.20060102150405000.zip
zipFileBase := fileBaseName + "." + getTimeStamp() + "." + "zip"
zipFilePath = filepath.Dir(filePath) + "/" + zipFileBase
} else {
zipFilePath = filePath + ".zip"
}
zipFile, err := os.OpenFile(zipFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0440)
if err != nil {
return err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
ofp, err := zipWriter.Create(filepath.Base(filePath))
if err != nil {
return err
}
_, err = io.Copy(ofp, ifp)
if err != nil {
return err
}
return nil
}
func shouldRollover(fPath string, MaxFileSize int) bool {
if MaxFileSize < 0 {
return false
}
fileInfo, err := os.Stat(fPath)
if err != nil {
Logger.Errorf(err, "state path: %s failed.", EscapPath(fPath))
return false
}
if fileInfo.Size() > int64(MaxFileSize*1024*1024) {
return true
}
return false
}
func doRollover(fPath string, MaxFileSize int, MaxBackupCount int) {
if !shouldRollover(fPath, MaxFileSize) {
return
}
timeStamp := getTimeStamp()
//absolute path
rotateFile := fPath + "." + timeStamp
err := CopyFile(fPath, rotateFile)
if err != nil {
Logger.Errorf(err, "copy path: %s failed.", EscapPath(fPath))
}
//truncate the file
f, err := os.OpenFile(fPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
Logger.Errorf(err, "truncate path: %s failed.", EscapPath(fPath))
return
}
f.Close()
//remove exceeded rotate files
removeExceededFiles(filepath.Dir(fPath), filepath.Base(fPath), MaxBackupCount, "rollover")
}
func doBackup(fPath string, MaxBackupCount int) {
if MaxBackupCount <= 0 {
return
}
pat := fmt.Sprintf(`%s\.[0-9]{1,17}$`, filepath.Base(fPath))
rotateFileList, err := FilterFileList(filepath.Dir(fPath), pat)
if err != nil {
Logger.Errorf(err, "walk path: %s failed.", EscapPath(fPath))
return
}
for _, file := range rotateFileList {
var err error
p := fmt.Sprintf(`%s\.[0-9]{17}$`, filepath.Base(fPath))
if ret, _ := regexp.MatchString(p, file); ret {
//svc.log.20060102150405000, not replace Timestamp
err = compressFile(file, filepath.Base(fPath), false)
} else {
//svc.log.1, replace Timestamp
err = compressFile(file, filepath.Base(fPath), true)
}
if err != nil {
Logger.Errorf(err, "compress path: %s failed.", EscapPath(file))
continue
}
err = removeFile(file)
if err != nil {
Logger.Errorf(err, "remove path %s failed.", EscapPath(file))
}
}
//remove exceeded backup files
removeExceededFiles(filepath.Dir(fPath), filepath.Base(fPath), MaxBackupCount, "backup")
}
func logRotateFile(file string, MaxFileSize int, MaxBackupCount int) {
defer func() {
if e := recover(); e != nil {
Logger.Errorf(nil, "LogRotate file path: %s catch an exception.", EscapPath(file))
}
}()
doRollover(file, MaxFileSize, MaxBackupCount)
doBackup(file, MaxBackupCount)
}
// LogRotate function for log rotate
// path: where log files need rollover
// MaxFileSize: MaxSize of a file before rotate. By M Bytes.
// MaxBackupCount: Max counts to keep of a log's backup files.
func LogRotate(path string, MaxFileSize int, MaxBackupCount int) {
//filter .log .trace files
defer func() {
if e := recover(); e != nil {
Logger.Errorf(nil, "LogRotate catch an exception")
}
}()
pat := `.(\.log|\.trace|\.out)$`
fileList, err := FilterFileList(path, pat)
if err != nil {
Logger.Errorf(err, "filepath.Walk() path: %s failed.", path)
return
}
for _, file := range fileList {
logRotateFile(file, MaxFileSize, MaxBackupCount)
}
}
// FilterFileList function for filter file list
//path : where the file will be filtered
//pat : regexp pattern to filter the matched file
func FilterFileList(path, pat string) ([]string, error) {
capacity := 10
//initialize a fileName slice, len=0, cap=10
fileList := make([]string, 0, capacity)
err := filepath.Walk(path,
func(pathName string, f os.FileInfo, e error) error {
if f == nil {
return e
}
if f.IsDir() {
return nil
}
if pat != "" {
ret, _ := regexp.MatchString(pat, f.Name())
if !ret {
return nil
}
}
fileList = append(fileList, pathName)
return nil
})
return fileList, err
}
// getTimeStamp get time stamp
func getTimeStamp() string {
now := time.Now().Format("2006.01.02.15.04.05.000")
timeSlot := strings.Replace(now, ".", "", -1)
return timeSlot
}
// CopyFile copy file
func CopyFile(srcFile, destFile string) error {
file, err := os.Open(srcFile)
if err != nil {
return err
}
defer file.Close()
dest, err := os.Create(destFile)
if err != nil {
return err
}
defer dest.Close()
_, err = io.Copy(dest, file)
return err
}
// initLogRotate initialize log rotate
func initLogRotate(logFilePath string, c *Lager) {
if logFilePath == "" {
return
}
checkConfig(c)
if c == nil {
go func() {
for {
LogRotate(filepath.Dir(logFilePath), LogRotateSize, LogBackupCount)
time.Sleep(30 * time.Second)
}
}()
} else {
if c.RollingPolicy == RollingPolicySize {
go func() {
for {
LogRotate(filepath.Dir(logFilePath), c.LogRotateSize, c.LogBackupCount)
time.Sleep(30 * time.Second)
}
}()
} else {
go func() {
for {
LogRotate(filepath.Dir(logFilePath), 0, c.LogBackupCount)
time.Sleep(24 * time.Hour * time.Duration(c.LogRotateDate))
}
}()
}
}
}
// checkPassLagerDefinition check pass lager definition
func checkConfig(c *Lager) {
if c.RollingPolicy == "" {
log.Println("RollingPolicy is empty, use default policy[size]")
c.RollingPolicy = RollingPolicySize
} else if c.RollingPolicy != "daily" && c.RollingPolicy != RollingPolicySize {
log.Printf("RollingPolicy is error, RollingPolicy=%s, use default policy[size].", c.RollingPolicy)
c.RollingPolicy = RollingPolicySize
}
if c.LogRotateDate <= 0 || c.LogRotateDate > 10 {
c.LogRotateDate = LogRotateDate
}
if c.LogRotateSize <= 0 || c.LogRotateSize > 50 {
c.LogRotateSize = LogRotateSize
}
if c.LogBackupCount < 0 || c.LogBackupCount > 100 {
c.LogBackupCount = LogBackupCount
}
}