-
Notifications
You must be signed in to change notification settings - Fork 14
/
rolling_writer.go
88 lines (73 loc) · 1.88 KB
/
rolling_writer.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
package logging
import (
"github.com/jitsucom/bulker/base/safego"
"io"
"path/filepath"
"sync/atomic"
"time"
"github.com/google/martian/log"
"gopkg.in/natefinch/lumberjack.v2"
)
const (
logFileMaxSizeMB = 100
twentyFourHoursInMinutes = 1440
)
// RollingWriterProxy for lumberjack.Logger
// Rotate() only if file isn't empty
type RollingWriterProxy struct {
lWriter *lumberjack.Logger
rotateOnClose bool
records uint64
}
func CreateLogWriter(config *Config) io.Writer {
if config.FileDir != GlobalType {
return NewRollingWriter(config)
} else {
return GlobalLogsWriter
}
}
func NewRollingWriter(config *Config) io.WriteCloser {
fileNamePath := filepath.Join(config.FileDir, config.FileName+".log")
lWriter := &lumberjack.Logger{
Filename: fileNamePath,
MaxSize: logFileMaxSizeMB,
Compress: config.Compress,
}
if config.MaxBackups > 0 {
lWriter.MaxBackups = config.MaxBackups
}
rwp := &RollingWriterProxy{lWriter: lWriter, records: 0, rotateOnClose: config.RotateOnClose}
if config.RotationMin == 0 {
config.RotationMin = twentyFourHoursInMinutes
}
rotation := time.Duration(config.RotationMin) * time.Minute
ticker := time.NewTicker(rotation)
safego.RunWithRestart(func() {
//initial rotate
if err := rwp.lWriter.Rotate(); err != nil {
log.Errorf("Error initial rotating log file [%s]: %v", rwp.lWriter.Filename, err)
}
for {
<-ticker.C
rwp.rotate()
}
})
return rwp
}
func (rwp *RollingWriterProxy) rotate() {
if atomic.SwapUint64(&rwp.records, 0) > 0 {
if err := rwp.lWriter.Rotate(); err != nil {
log.Errorf("Error rotating log file [%s]: %v", rwp.lWriter.Filename, err)
}
}
}
func (rwp *RollingWriterProxy) Write(p []byte) (int, error) {
atomic.AddUint64(&rwp.records, 1)
return rwp.lWriter.Write(p)
}
func (rwp *RollingWriterProxy) Close() error {
if rwp.rotateOnClose {
rwp.rotate()
}
return rwp.lWriter.Close()
}