-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_rotator.go
160 lines (134 loc) · 3.4 KB
/
file_rotator.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
package logp
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
const RotatorMaxFiles = 1024
const DefaultKeepFiles = 7
const DefaultRotateEveryBytes = 10 * 1024 * 1024
type FileRotator struct {
Path string
Name string
RotateEveryBytes *uint64
KeepFiles *int
current *os.File
current_size uint64
}
func (rotator *FileRotator) CreateDirectory() error {
fileinfo, err := os.Stat(rotator.Path)
if err == nil {
if !fileinfo.IsDir() {
return fmt.Errorf("%s exists but it's not a directory", rotator.Path)
}
}
if os.IsNotExist(err) {
err = os.MkdirAll(rotator.Path, 0755)
if err != nil {
return err
}
}
return nil
}
func (rotator *FileRotator) CheckIfConfigSane() error {
if len(rotator.Name) == 0 {
return fmt.Errorf("File logging requires a name for the file names")
}
if rotator.KeepFiles == nil {
rotator.KeepFiles = new(int)
*rotator.KeepFiles = DefaultKeepFiles
}
if rotator.RotateEveryBytes == nil {
rotator.RotateEveryBytes = new(uint64)
*rotator.RotateEveryBytes = DefaultRotateEveryBytes
}
if *rotator.KeepFiles < 2 || *rotator.KeepFiles >= RotatorMaxFiles {
return fmt.Errorf("The number of files to keep should be between 2 and %d", RotatorMaxFiles-1)
}
return nil
}
func (rotator *FileRotator) WriteLine(line []byte) error {
if rotator.shouldRotate() {
err := rotator.Rotate()
if err != nil {
return err
}
}
line = append(line, '\n')
_, err := rotator.current.Write(line)
if err != nil {
return err
}
rotator.current_size += uint64(len(line))
return nil
}
func (rotator *FileRotator) shouldRotate() bool {
if rotator.current == nil {
return true
}
if rotator.current_size >= *rotator.RotateEveryBytes {
return true
}
return false
}
func (rotator *FileRotator) FilePath(file_no int) string {
if file_no == 0 {
return filepath.Join(rotator.Path, rotator.Name)
}
filename := strings.Join([]string{rotator.Name, strconv.Itoa(file_no)}, ".")
return filepath.Join(rotator.Path, filename)
}
func (rotator *FileRotator) FileExists(file_no int) bool {
file_path := rotator.FilePath(file_no)
_, err := os.Stat(file_path)
if os.IsNotExist(err) {
return false
}
return true
}
func (rotator *FileRotator) Rotate() error {
if rotator.current != nil {
if err := rotator.current.Close(); err != nil {
return err
}
}
// delete any extra files, normally we shouldn't have any
for file_no := *rotator.KeepFiles; file_no < RotatorMaxFiles; file_no++ {
if rotator.FileExists(file_no) {
perr := os.Remove(rotator.FilePath(file_no))
if perr != nil {
return perr
}
}
}
// shift all files from last to first
for file_no := *rotator.KeepFiles - 1; file_no >= 0; file_no-- {
if !rotator.FileExists(file_no) {
// file doesn't exist, don't rotate
continue
}
file_path := rotator.FilePath(file_no)
if rotator.FileExists(file_no + 1) {
// next file exists, something is strange
return fmt.Errorf("File %s exists, when rotating would overwrite it", rotator.FilePath(file_no+1))
}
err := os.Rename(file_path, rotator.FilePath(file_no+1))
if err != nil {
return err
}
}
// create the new file
file_path := rotator.FilePath(0)
current, err := os.Create(file_path)
if err != nil {
return err
}
rotator.current = current
rotator.current_size = 0
// delete the extra file, ignore errors here
file_path = rotator.FilePath(*rotator.KeepFiles)
os.Remove(file_path)
return nil
}