-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathrotator.go
551 lines (467 loc) · 14 KB
/
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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 file
import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"sync"
"time"
"errors"
)
const (
// MaxBackupsLimit is the upper bound on the number of backup files. Any values
// greater will result in an error.
MaxBackupsLimit = 1024
DateFormat = "20060102"
)
// rotater is the interface responsible for rotating and finding files.
type rotater interface {
// ActiveFile returns the path to the file that is actively written.
ActiveFile() string
// RotatedFiles returns the list of rotated files. The oldest comes first.
RotatedFiles() []string
// Rotate rotates the file.
Rotate(reason rotateReason, rotateTime time.Time) error
}
// Rotator is a io.WriteCloser that automatically rotates the file it is
// writing to when it reaches a maximum size and optionally on a time interval
// basis. It also purges the oldest rotated files when the maximum number of
// backups is reached.
type Rotator struct {
rot rotater
triggers []trigger
filename string
maxSizeBytes uint
maxBackups uint
interval time.Duration
permissions os.FileMode
log Logger // Optional Logger (may be nil).
rotateOnStartup bool
redirectStderr bool
clock clock
file *os.File
mutex sync.Mutex
}
// Logger allows the rotator to write debug information.
type Logger interface {
Debugw(msg string, keysAndValues ...interface{}) // Debug
}
// RotatorOption is a configuration option for Rotator.
type RotatorOption func(r *Rotator)
// MaxSizeBytes configures the maximum number of bytes that a file should
// contain before being rotated. The default is 10 MiB.
func MaxSizeBytes(n uint) RotatorOption {
return func(r *Rotator) {
r.maxSizeBytes = n
}
}
// MaxBackups configures the maximum number of backup files to save (not
// counting the active file). The upper limit is 1024 on this value is.
// The default is 7.
func MaxBackups(n uint) RotatorOption {
return func(r *Rotator) {
r.maxBackups = n
}
}
// Permissions configures the file permissions to use for the file that
// the Rotator creates. The default is 0600.
func Permissions(m os.FileMode) RotatorOption {
return func(r *Rotator) {
r.permissions = m
}
}
// WithLogger injects a logger implementation for logging debug information.
// If no logger is injected then the no logging will occur.
func WithLogger(l Logger) RotatorOption {
return func(r *Rotator) {
r.log = l
}
}
// Interval sets the time interval for log rotation in addition to log
// rotation by size. The default is 0 for disabled.
func Interval(d time.Duration) RotatorOption {
return func(r *Rotator) {
r.interval = d
}
}
// RotateOnStartup immediately rotates files on startup rather than appending to
// the existing file. The default is true.
func RotateOnStartup(b bool) RotatorOption {
return func(r *Rotator) {
r.rotateOnStartup = b
}
}
// RedirectStderr causes all writes to standard error to be redirected
// to this rotator.
func RedirectStderr(redirect bool) RotatorOption {
return func(r *Rotator) {
r.redirectStderr = redirect
}
}
func WithClock(clock clock) RotatorOption {
return func(r *Rotator) {
r.clock = clock
}
}
// NewFileRotator returns a new Rotator.
func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error) {
r := &Rotator{
maxSizeBytes: 10 * 1024 * 1024, // 10 MiB
maxBackups: 7,
permissions: 0600,
interval: 0,
rotateOnStartup: true,
clock: &realClock{},
}
for _, opt := range options {
opt(r)
}
if r.maxSizeBytes == 0 {
return nil, errors.New("file rotator max file size must be greater than 0")
}
if r.maxBackups > MaxBackupsLimit {
return nil, fmt.Errorf("file rotator max backups %d is greater than the limit of %v", r.maxBackups, MaxBackupsLimit)
}
if r.permissions > os.ModePerm {
return nil, fmt.Errorf("file rotator permissions mask of %o is invalid", r.permissions)
}
if r.interval != 0 && r.interval < time.Second {
return nil, errors.New("the minimum time interval for log rotation is 1 second")
}
r.rot = newDateRotater(r.log, filename, r.clock)
shouldRotateOnStart := r.rotateOnStartup
if _, err := os.Stat(r.rot.ActiveFile()); os.IsNotExist(err) {
shouldRotateOnStart = false
}
r.triggers = newTriggers(shouldRotateOnStart, r.interval, r.maxSizeBytes, r.clock)
if r.log != nil {
r.log.Debugw("Initialized file rotator",
"filename", r.filename,
"max_size_bytes", r.maxSizeBytes,
"max_backups", r.maxBackups,
"permissions", r.permissions,
)
}
return r, nil
}
// Write writes the given bytes to the file. This implements io.Writer. If
// the write would trigger a rotation the rotation is done before writing to
// avoid going over the max size. Write is safe for concurrent use.
func (r *Rotator) Write(data []byte) (int, error) {
r.mutex.Lock()
defer r.mutex.Unlock()
dataLen := uint(len(data))
if dataLen > r.maxSizeBytes {
return 0, fmt.Errorf("data size (%d bytes) is greater than "+
"the max file size (%d bytes)", dataLen, r.maxSizeBytes)
}
if r.file == nil {
if err := r.openNew(); err != nil {
return 0, fmt.Errorf("failed to open new log file for writing: %w", err)
}
} else {
if reason, t := r.isRotationTriggered(dataLen); reason != rotateReasonNoRotate {
if err := r.rotateWithTime(reason, t); err != nil {
return 0, fmt.Errorf("error file rotating files reason: %s: %w", reason, err)
}
if err := r.openFile(); err != nil {
return 0, fmt.Errorf("failed to open existing log file for writing: %w", err)
}
}
}
n, err := r.file.Write(data)
if err != nil {
return n, fmt.Errorf("failed to write to file: %w", err)
}
return n, nil
}
// openNew opens r's log file for the first time, creating it if it doesn't
// exist.
func (r *Rotator) openNew() error {
err := os.MkdirAll(r.dir(), r.dirMode())
if err != nil {
return fmt.Errorf("failed to make directories for new file: %w", err)
}
_, err = os.Stat(r.rot.ActiveFile())
if err == nil {
// check if the file has to be rotated before writing to it
reason, t := r.isRotationTriggered(0)
if reason == rotateReasonNoRotate {
return r.appendToFile()
}
if err = r.rot.Rotate(reason, t); err != nil {
return fmt.Errorf("failed to rotate backups: %w", err)
}
if err = r.purge(); err != nil {
return fmt.Errorf("failed to purge unnecessary rotated files: %w", err)
}
}
return r.openFile()
}
// appendToFile opens an existing log file for appending. Unlike openFile it
// does not call MkdirAll because it is an error for the file to not already
// exist.
func (r *Rotator) appendToFile() error {
var err error
r.file, err = os.OpenFile(r.rot.ActiveFile(), os.O_WRONLY|os.O_APPEND, r.permissions)
if err != nil {
return fmt.Errorf("failed to append to existing file: %w", err)
}
if r.redirectStderr {
_ = RedirectStandardError(r.file)
}
return nil
}
func (r *Rotator) openFile() error {
err := os.MkdirAll(r.dir(), r.dirMode())
if err != nil {
return fmt.Errorf("failed to make directories for new file: %w", err)
}
r.file, err = os.OpenFile(r.rot.ActiveFile(), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, r.permissions)
if err != nil {
return fmt.Errorf("failed to open new file '%s': %w", r.rot.ActiveFile(), err)
}
if r.redirectStderr {
_ = RedirectStandardError(r.file)
}
return nil
}
func (r *Rotator) rotate(reason rotateReason) error {
return r.rotateWithTime(reason, r.clock.Now())
}
// rotateWithTime closes the actively written file, and rotates it along with existing
// rotated files if needed. When it is done, unnecessary files are removed.
func (r *Rotator) rotateWithTime(reason rotateReason, rotationTime time.Time) error {
if err := r.closeFile(); err != nil {
return fmt.Errorf("error file closing current file: %w", err)
}
if err := r.rot.Rotate(reason, rotationTime); err != nil {
return fmt.Errorf("failed to rotate backups: %w", err)
}
return r.purge()
}
func (r *Rotator) purge() error {
rotatedFiles := r.rot.RotatedFiles()
count := uint(len(rotatedFiles))
if count <= r.maxBackups {
return nil
}
purgeUntil := count - r.maxBackups
filesToPurge := rotatedFiles[:purgeUntil]
for _, name := range filesToPurge {
_, err := os.Stat(name)
switch {
case err == nil:
if err = os.Remove(name); err != nil {
return fmt.Errorf("failed to delete %v during rotation: %w", name, err)
}
case os.IsNotExist(err):
return nil
default:
return fmt.Errorf("failed on %v during rotation: %w", name, err)
}
}
return nil
}
func (r *Rotator) isRotationTriggered(dataLen uint) (rotateReason, time.Time) {
for _, t := range r.triggers {
reason := t.TriggerRotation(dataLen)
if reason != rotateReasonNoRotate {
return reason, r.clock.Now()
}
}
return rotateReasonNoRotate, time.Time{}
}
// Sync commits the current contents of the file to stable storage. Typically,
// this means flushing the file system's in-memory copy of recently written data
// to disk.
func (r *Rotator) Sync() error {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.file == nil {
return nil
}
return r.file.Sync()
}
// Rotate triggers a file rotation.
func (r *Rotator) Rotate() error {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.rotate(rotateReasonManualTrigger)
}
// Close closes the currently open file.
func (r *Rotator) Close() error {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.closeFile()
}
func (r *Rotator) dir() string {
return filepath.Dir(r.rot.ActiveFile())
}
func (r *Rotator) dirMode() os.FileMode {
mode := 0700
if r.permissions&0070 > 0 {
mode |= 0050
}
if r.permissions&0007 > 0 {
mode |= 0005
}
return os.FileMode(mode)
}
func (r *Rotator) closeFile() error {
if r.file == nil {
return nil
}
err := r.file.Close()
r.file = nil
if err != nil {
return fmt.Errorf("failed to close active file: %w", err)
}
return nil
}
type dateRotator struct {
log Logger
clock clock
format string
filenamePrefix string
currentFilename string
extension string
prefixLen int
filenameLen int
extensionLen int
// logOrderCache is used to cache log file meta information between rotations
logOrderCache map[string]logOrder
}
func newDateRotater(log Logger, filename string, clock clock) rotater {
d := &dateRotator{
log: log,
clock: clock,
filenamePrefix: filename + "-",
extension: ".ndjson",
format: DateFormat,
logOrderCache: make(map[string]logOrder),
}
d.prefixLen = len(d.filenamePrefix)
d.filenameLen = d.prefixLen + len(DateFormat)
d.extensionLen = len(d.extension)
d.currentFilename = d.filenamePrefix + d.clock.Now().Format(d.format) + d.extension
files, err := filepath.Glob(d.filenamePrefix + "*" + d.extension)
if err != nil {
return d
}
// continue from last file
if len(files) != 0 {
if len(files) == 1 {
d.currentFilename = files[0]
} else {
d.SortModTimeLogs(files)
d.currentFilename = files[len(files)-1]
}
}
return d
}
func (d *dateRotator) ActiveFile() string {
return d.currentFilename
}
func (d *dateRotator) Rotate(reason rotateReason, rotateTime time.Time) error {
if d.log != nil {
d.log.Debugw("Rotating file", "filename", d.currentFilename, "reason", reason)
}
d.logOrderCache = make(map[string]logOrder, 0)
newFileNamePrefix := d.filenamePrefix + rotateTime.Format(d.format)
files, err := filepath.Glob(newFileNamePrefix + "*" + d.extension)
if err != nil {
return fmt.Errorf("failed to get possible files: %w", err)
}
if len(files) == 0 {
d.currentFilename = newFileNamePrefix + d.extension
return nil
}
d.SortModTimeLogs(files)
order := d.OrderLog(files[len(files)-1])
d.currentFilename = newFileNamePrefix + "-" + strconv.Itoa(order.index+1) + d.extension
return nil
}
func (d *dateRotator) RotatedFiles() []string {
files, err := filepath.Glob(d.filenamePrefix + "*")
if err != nil {
if d.log != nil {
d.log.Debugw("failed to list existing logs: %+v", err)
}
}
for i, name := range files {
if name == d.ActiveFile() {
files = append(files[:i], files[i+1:]...)
break
}
}
d.SortModTimeLogs(files)
return files
}
// SortModTimeLogs puts newest file to the last
func (d *dateRotator) SortModTimeLogs(strings []string) {
sort.Slice(
strings,
func(i, j int) bool {
return d.OrderLog(strings[i]).After(d.OrderLog(strings[j]))
},
)
}
// logOrder stores information required to sort log files
// parsed out from the following format {filename}-{datetime}-{index}.ndjson
type logOrder struct {
index int
datetime time.Time
}
func (o logOrder) After(other logOrder) bool {
if o.datetime.Equal(other.datetime) {
return other.index > o.index
}
return !o.datetime.After(other.datetime)
}
func (d *dateRotator) OrderLog(filename string) logOrder {
if o, ok := d.logOrderCache[filename]; ok {
return o
}
var o logOrder
var err error
o.datetime, err = time.Parse(d.format, filename[d.prefixLen:d.filenameLen])
if err != nil {
return o
}
if d.isFilenameWithIndex(filename) {
o.index, err = d.filenameIndex(filename)
if err != nil {
return o
}
}
d.logOrderCache[filename] = o
return o
}
func (d *dateRotator) isFilenameWithIndex(filename string) bool {
return d.filenameLen+d.extensionLen < len(filename)
}
func (d *dateRotator) filenameIndex(filename string) (int, error) {
indexStr := filename[d.filenameLen+1 : len(filename)-d.extensionLen]
if len(indexStr) > 0 {
return strconv.Atoi(indexStr)
}
return 0, nil
}