-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimerotation.go
66 lines (61 loc) · 1.52 KB
/
timerotation.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
package logging
import (
"io"
"os"
"strings"
"time"
)
type TimeRotationHandler struct {
*Handler
localData map[string]string
}
func NewTimeRotationHandler(shortfile string, suffix string) (*TimeRotationHandler, error) {
h := &TimeRotationHandler{}
fullfile := strings.Join([]string{shortfile, time.Now().Format(suffix)}, ".")
file, err := h.openFile(fullfile, shortfile)
if err != nil {
return nil, err
}
h.Handler = NewHandler(file)
h.before = h.rotate
h.localData = make(map[string]string)
h.localData["oldfilepath"] = fullfile
h.localData["linkpath"] = shortfile
h.localData["suffix"] = suffix
return h, nil
}
func (h *TimeRotationHandler) openFile(filepath, linkpath string) (*os.File, error) {
if _, err := os.Stat(filepath); err != nil {
if os.IsNotExist(err) {
if _, err := os.Create(filepath); err != nil {
return nil, err
}
} else {
return nil, err
}
}
_ = os.Remove(linkpath)
var fn string
if err := os.Symlink(filepath, linkpath); err != nil {
fn = filepath
} else {
fn = linkpath
}
file, err := os.OpenFile(fn, FileCreateFlag, FileCreatePerm)
if err != nil {
return nil, err
}
return file, nil
}
func (h *TimeRotationHandler) rotate(*Record, io.ReadWriter) {
filepath := h.localData["linkpath"] + "." + time.Now().Format(h.localData["suffix"])
if filepath != h.localData["oldfilepath"] {
_ = h.writer.(io.Closer).Close()
file, err := h.openFile(filepath, h.localData["linkpath"])
if err != nil {
return
}
h.writer = file
h.localData["oldfilepath"] = filepath
}
}