forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
file_rotator.go
160 lines (134 loc) · 3.38 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
currentSize 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, 0750)
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.currentSize += uint64(len(line))
return nil
}
func (rotator *FileRotator) shouldRotate() bool {
if rotator.current == nil {
return true
}
if rotator.currentSize >= *rotator.RotateEveryBytes {
return true
}
return false
}
func (rotator *FileRotator) FilePath(fileNo int) string {
if fileNo == 0 {
return filepath.Join(rotator.Path, rotator.Name)
}
filename := strings.Join([]string{rotator.Name, strconv.Itoa(fileNo)}, ".")
return filepath.Join(rotator.Path, filename)
}
func (rotator *FileRotator) FileExists(fileNo int) bool {
path := rotator.FilePath(fileNo)
_, err := os.Stat(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 fileNo := *rotator.KeepFiles; fileNo < RotatorMaxFiles; fileNo++ {
if rotator.FileExists(fileNo) {
perr := os.Remove(rotator.FilePath(fileNo))
if perr != nil {
return perr
}
}
}
// shift all files from last to first
for fileNo := *rotator.KeepFiles - 1; fileNo >= 0; fileNo-- {
if !rotator.FileExists(fileNo) {
// file doesn't exist, don't rotate
continue
}
path := rotator.FilePath(fileNo)
if rotator.FileExists(fileNo + 1) {
// next file exists, something is strange
return fmt.Errorf("File %s exists, when rotating would overwrite it", rotator.FilePath(fileNo+1))
}
err := os.Rename(path, rotator.FilePath(fileNo+1))
if err != nil {
return err
}
}
// create the new file
path := rotator.FilePath(0)
current, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
rotator.current = current
rotator.currentSize = 0
// delete the extra file, ignore errors here
path = rotator.FilePath(*rotator.KeepFiles)
os.Remove(path)
return nil
}