forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_logrus_hook.go
58 lines (45 loc) · 980 Bytes
/
redis_logrus_hook.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
package main
import (
"fmt"
"github.com/Sirupsen/logrus"
"time"
)
type redisChannelHook struct {
Notifier RedisNotificationHandler
formatter logrus.Formatter
}
func NewRedisHook() *redisChannelHook {
hook := &redisChannelHook{}
hook.formatter = new(logrus.JSONFormatter)
hook.Notifier = RedisNotificationHandler{}
hook.Notifier.Start()
return hook
}
func (hook *redisChannelHook) Fire(entry *logrus.Entry) error {
orgId, found := entry.Data["org_id"]
if !found {
return nil
}
newEntry, fmtErr := hook.formatter.Format(entry)
if fmtErr != nil {
fmt.Println(fmtErr)
return nil
}
msg := string(newEntry)
n := InterfaceNotification{
Type: "gateway-log",
Message: msg,
OrgID: orgId.(string),
Timestamp: time.Now(),
}
go hook.Notifier.Notify(n)
return nil
}
func (hook *redisChannelHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.InfoLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}