forked from gogap/logrus_mate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotatelogs.go
69 lines (53 loc) · 1.32 KB
/
rotatelogs.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
package redisio
import (
"errors"
"io"
"time"
"github.com/gogap/config"
"github.com/klahdenk/logrus_mate"
"github.com/lestrrat-go/file-rotatelogs"
)
type RedisIOConfig struct {
Network string
Address string
Password string
Db int64
ListName string
}
func init() {
logrus_mate.RegisterWriter("rotatelogs", NewRotateLogs)
}
func NewRotateLogs(conf config.Configuration) (writer io.Writer, err error) {
path := conf.GetString("path")
if len(path) == 0 {
err = errors.New("config of path is empty, e.g.: /path/to/access_log.%Y%m%d%H%M")
return
}
clock := toClock(conf.GetString("clock", "Local"))
strLoc := conf.GetString("location", time.Now().Local().Location().String())
loc, err := time.LoadLocation(strLoc)
if err != nil {
return
}
linkName := conf.GetString("link-name")
rotationTime := conf.GetTimeDuration("rotation-time", time.Hour*24)
maxAge := conf.GetTimeDuration("max-age", time.Hour*24*7)
log, err := rotatelogs.New(path,
rotatelogs.WithClock(clock),
rotatelogs.WithLocation(loc),
rotatelogs.WithLinkName(linkName),
rotatelogs.WithRotationTime(rotationTime),
rotatelogs.WithMaxAge(maxAge),
)
if err != nil {
return
}
writer = log
return
}
func toClock(name string) rotatelogs.Clock {
if name == "UTC" || name == "utc" {
return rotatelogs.UTC
}
return rotatelogs.Local
}