forked from TykTechnologies/tyk
/
redis_logrus_hook.go
64 lines (51 loc) · 1.14 KB
/
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
59
60
61
62
63
64
package main
import (
"time"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/storage"
)
type redisChannelHook struct {
notifier RedisNotifier
formatter logrus.Formatter
}
func newRedisHook() *redisChannelHook {
hook := &redisChannelHook{}
hook.formatter = new(logrus.JSONFormatter)
hook.notifier.store = storage.RedisCluster{KeyPrefix: "gateway-notifications:"}
hook.notifier.channel = "dashboard.ui.messages"
return hook
}
func (hook *redisChannelHook) Fire(entry *logrus.Entry) error {
orgId, found := entry.Data["org_id"]
if !found {
return nil
}
newEntry, err := hook.formatter.Format(entry)
if err != nil {
log.Error(err)
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
}
type InterfaceNotification struct {
Type string
Message string
OrgID string
Timestamp time.Time
}
func (hook *redisChannelHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.InfoLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}