-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.go
68 lines (65 loc) · 1.45 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
package jlog
import (
"compress/flate"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
)
func gzipFile(filename string) {
inputFile, err := os.Open(filename)
if err != nil {
return
}
newName := filename + gzSuffix
outputFile, err := os.Create(newName)
if err != nil {
_ = inputFile.Close()
_ = os.Rename(filename, newName)
return
}
defer func() {
_ = inputFile.Close()
_ = outputFile.Close()
if err != nil {
_ = os.Rename(filename, newName)
} else {
fmt.Println("remove src file:", filename)
os.Remove(filename)
}
}()
gzipWriter, err := gzip.NewWriterLevel(outputFile, flate.BestCompression)
if err != nil {
return
}
defer gzipWriter.Close()
_, err = io.Copy(gzipWriter, inputFile)
if err != nil {
return
}
}
func rotateLog(s severity) {
for i := logCount; i >= 1; i-- {
newName := filepath.Join(logDir, processName+severityName[s]+"."+fmt.Sprintf("%d", i))
if compress {
newName += gzSuffix
}
if i == logCount {
_ = os.Remove(newName)
// fmt.Println("remove", newName)
}
oldName := filepath.Join(logDir, processName+severityName[s]+"."+fmt.Sprintf("%d", i-1))
if compress {
oldName += gzSuffix
}
_ = os.Rename(oldName, newName)
//fmt.Println("rename", oldName, "to", newName)
}
oldName := filepath.Join(logDir, processName+severityName[s]+".temp")
newName := filepath.Join(logDir, processName+severityName[s]+".0")
_ = os.Rename(oldName, newName)
if compress {
gzipFile(newName)
}
}