/
rotatinglogger.go
77 lines (70 loc) · 1.48 KB
/
rotatinglogger.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
package alog
import (
"errors"
"os"
"path/filepath"
)
const ROTATE_SIZE = 10 * (1 << 20)
type RotatingLogger struct {
*Logger
loggerInt PrintLogger
path string
file *os.File
size int64
}
func NewRotatingLogger(path string, loggerInt PrintLogger) (*RotatingLogger, error) {
var err error
l := &RotatingLogger{}
l.path = path
l.loggerInt = loggerInt
stat, err := os.Stat(l.path)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
// Make the directory, in case it doesn't exist yet
os.MkdirAll(filepath.Dir(path), 0755)
} else {
l.size = stat.Size()
}
l.Logger = New(l, "@(dim:{isodate}) ", 0)
err = l.openfile()
if err != nil {
return nil, err
}
return l, nil
}
func (l *RotatingLogger) rotate() {
os.Rename(l.path, l.path+".old")
err := l.openfile()
if err != nil {
l.loggerInt.Printf("(@error:Error opening new log file %s on rotation: %v)\n", l.path, err)
}
l.size = 0
}
func (l *RotatingLogger) openfile() error {
var err error
if l.file != nil {
go l.file.Close()
}
l.file, err = os.OpenFile(l.path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
return nil
}
func (l *RotatingLogger) Write(buf []byte) (int, error) {
// Println(l)
if l.file == nil {
return 0, errors.New("Logfile not open")
}
nn, err := l.file.Write(buf)
l.size += int64(nn)
if l.size > ROTATE_SIZE {
l.rotate()
}
if err != nil {
l.loggerInt.Printf("(@error:Error writing to log file: %v)\n", err)
}
return nn, err
}