-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (41 loc) · 1020 Bytes
/
main.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
package logrusredis
import (
"github.com/Sirupsen/logrus"
"github.com/go-redis/redis"
"time"
)
// LogrusRedis delivers logs to a Redis List
type LogrusRedis struct {
client *redis.Client
key string
rangeCursor int64
len int64
Expire time.Duration
formatter *logrus.TextFormatter
}
// Fire adds logrus entry into redis list
func (r *LogrusRedis) Fire(entry *logrus.Entry) error {
body, err := r.formatter.Format(entry)
if err != nil {
return err
}
err = r.client.RPush(r.key, body).Err()
if err != nil {
return err
}
err = r.client.Expire(r.key, r.Expire).Err()
return err
}
// Levels returns the available logging levels.
func (r *LogrusRedis) Levels() []logrus.Level {
return logrus.AllLevels
}
// NewLogrusRedis creates LogrusRedis instance
func NewLogrusRedis(client *redis.Client, key string) *LogrusRedis {
return &LogrusRedis{
client: client,
key: key,
Expire: time.Hour,
formatter: &logrus.TextFormatter{DisableColors: true},
}
}