-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomrot.go
More file actions
273 lines (232 loc) · 5.43 KB
/
comrot.go
File metadata and controls
273 lines (232 loc) · 5.43 KB
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
// Comrot provides a compressed log rotation writer.
// It satisfies the io.WriteCloser interface.
package comrot
import (
"bufio"
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"sync"
"time"
)
const (
MaxInt = int(^uint(0) >> 1)
MB = 1 << (10 * 2)
TimeFmt = time.RFC3339
)
var (
DefaultMaxSize = 10 * MB
DefaultMaxFiles = MaxInt
)
// Ensure we always implement io.WriteCloser
var _ io.WriteCloser = (*RotateWriter)(nil)
// RotateWriter is the (compressed) rotated log writer.
type RotateWriter struct {
// Filename is the name of the file to write to.
filename string
// MaxSize is the threshold in MB of the log file size.
// Once exceeded the writer rotates. It defaults to 10 MB.
MaxSize int
// MaxFiles is the maximum number of rotated files
// to retain. Default is infinite.
MaxFiles int
// Compress is the flag indicating whether rotated
// files should be compressed or not. Default is true.
Compress bool
// fp is the handle to the current log file.
fp *os.File
// fsize caches the current log file size.
fsize int64
// mu syncs writer ops.
mu sync.Mutex
}
// NewRotateWriter creates a new RotateWriter.
// Returns nil if an error occurs during setup.
func NewRotateWriter(filename string) *RotateWriter {
w := &RotateWriter{
filename: filename,
MaxSize: DefaultMaxSize,
MaxFiles: DefaultMaxFiles,
Compress: true,
}
err := w.Open()
if err != nil {
return nil
}
return w
}
// Write satisfies the io.Writer interface.
func (w *RotateWriter) Write(out []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
// Open if closed.
if w.fp == nil {
w.open()
}
// Rotate if write exceeds threshold.
if w.fsize+int64(len(out)) > int64(w.MaxSize) {
w.rotate()
}
return w.write(out)
}
// write actually writes to the log.
func (w *RotateWriter) write(out []byte) (int, error) {
n, err := w.fp.Write(out)
w.fsize += int64(n)
return n, err
}
// Close statisfies the io.Closer interface.
func (w *RotateWriter) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
return w.close()
}
// close actually closes the writer.
func (w *RotateWriter) close() error {
if w.fp == nil {
return nil
}
w.fp = nil
return w.fp.Close()
}
// Open opens the log file if it exists. Creates a
// new file otherwise.
func (w *RotateWriter) Open() error {
w.mu.Lock()
defer w.mu.Unlock()
return w.open()
}
// open opens or creates the log file.
func (w *RotateWriter) open() error {
info, err := os.Stat(w.filename)
if os.IsNotExist(err) {
w.fp, err = os.Create(w.filename)
w.fsize = int64(0)
return err
}
w.fp, err = os.OpenFile(w.filename, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
w.fsize = info.Size()
return nil
}
// Rotate performs the rotation and creation of files.
func (w *RotateWriter) Rotate() (err error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.rotate()
}
// rotate actually rotates the log file.
func (w *RotateWriter) rotate() (err error) {
// Close existing file if open.
if w.fp != nil {
err = w.fp.Close()
w.fp = nil
if err != nil {
return
}
}
// Rename dest file if it already exists.
_, err = os.Stat(w.filename)
if err == nil {
rot := w.filename + "." + time.Now().Format(TimeFmt)
err = os.Rename(w.filename, rot)
if err != nil {
return err
}
if w.Compress {
err = w.compress(rot) // TODO: async
if err != nil {
return err
}
}
}
// Clean up old.
w.drain()
// Create new.
return w.open()
}
// compress compresses a file with gzip algorithm.
func (w *RotateWriter) compress(source string) (err error) {
// Read uncompressed file.
rawfile, err := os.Open(source)
if err != nil {
return err
}
defer rawfile.Close()
// Calculate the buffer size.
info, _ := rawfile.Stat()
rawbytes := make([]byte, info.Size())
// Read rawfile content into buffer.
buffer := bufio.NewReader(rawfile)
_, err = buffer.Read(rawbytes)
if err != nil {
return err
}
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
writer.Write(rawbytes)
writer.Close()
err = ioutil.WriteFile(source+".gz", buf.Bytes(), info.Mode())
if err != nil {
return err
}
// Remove uncompressed.
go os.Remove(source)
return nil
}
// drain cleans old and archived files.
func (w *RotateWriter) drain() {
if w.MaxFiles == MaxInt {
return
}
files, err := ioutil.ReadDir(filepath.Dir(w.filename))
if err != nil {
return
}
// Collect log fragments.
frags := []fragInfo{}
for _, f := range files {
if f.IsDir() {
continue
}
if f.Name() == w.filename {
continue
}
base, file := filepath.Base(w.filename), f.Name()
// Extract timestamp from filename.
ts := file[len(base)+1 : len(base)+1+len(TimeFmt)]
t, err := time.Parse(time.RFC3339, ts)
if err == nil {
frags = append(frags, fragInfo{t, f})
}
}
sort.Sort(byTime(frags))
// Collect deletable fragmets.
deletes := []fragInfo{}
if w.MaxFiles < len(frags) {
deletes = frags[w.MaxFiles:]
frags = frags[:w.MaxFiles]
}
go func(fs []fragInfo) {
for _, f := range fs {
os.Remove(filepath.Join(filepath.Dir(w.filename), f.Name()))
}
}(deletes)
}
// fragInfo is a log fragment with file info.
type fragInfo struct {
t time.Time
os.FileInfo
}
// byTime implements sort.Interface for []fragInfo.
// Sorts based on the time field and in descending order.
type byTime []fragInfo
func (b byTime) Len() int { return len(b) }
func (b byTime) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byTime) Less(i, j int) bool { return b[i].t.After(b[j].t) }