-
Notifications
You must be signed in to change notification settings - Fork 13
/
logrus_redis.go
192 lines (163 loc) · 4.5 KB
/
logrus_redis.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package logredis
import (
"encoding/json"
"fmt"
"time"
"github.com/garyburd/redigo/redis"
"github.com/sirupsen/logrus"
)
// HookConfig stores configuration needed to setup the hook
type HookConfig struct {
Key string
Format string
App string
Host string
Password string
Hostname string
Port int
DB int
TTL int
}
// RedisHook to sends logs to Redis server
type RedisHook struct {
RedisPool *redis.Pool
RedisHost string
RedisKey string
LogstashFormat string
AppName string
Hostname string
RedisPort int
TTL int
DialOptions []redis.DialOption
}
// NewHook creates a hook to be added to an instance of logger
func NewHook(config HookConfig, options ...redis.DialOption) (*RedisHook, error) {
pool := newRedisConnectionPool(config.Host, config.Password, config.Port, config.DB, options...)
if config.Format != "v0" && config.Format != "v1" && config.Format != "access" {
return nil, fmt.Errorf("unknown message format")
}
// test if connection with REDIS can be established
conn := pool.Get()
defer conn.Close()
// check connection
_, err := conn.Do("PING")
if err != nil {
return nil, fmt.Errorf("unable to connect to REDIS: %s", err)
}
return &RedisHook{
RedisHost: config.Host,
RedisPool: pool,
RedisKey: config.Key,
LogstashFormat: config.Format,
AppName: config.App,
Hostname: config.Hostname,
TTL: config.TTL,
DialOptions: options,
}, nil
}
// Fire is called when a log event is fired.
func (hook *RedisHook) Fire(entry *logrus.Entry) error {
var msg interface{}
switch hook.LogstashFormat {
case "v0":
msg = createV0Message(entry, hook.AppName, hook.Hostname)
case "v1":
msg = createV1Message(entry, hook.AppName, hook.Hostname)
case "access":
msg = createAccessLogMessage(entry, hook.AppName, hook.Hostname)
default:
fmt.Println("Invalid LogstashFormat")
}
// Marshal into json message
js, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("error creating message for REDIS: %s", err)
}
// get connection from pool
conn := hook.RedisPool.Get()
defer conn.Close()
// send message
_, err = conn.Do("RPUSH", hook.RedisKey, js)
if err != nil {
return fmt.Errorf("error sending message to REDIS: %s", err)
}
if hook.TTL != 0 {
_, err = conn.Do("EXPIRE", hook.RedisKey, hook.TTL)
if err != nil {
return fmt.Errorf("error setting TTL to key: %s, %s", hook.RedisKey, err)
}
}
return nil
}
// Levels returns the available logging levels.
func (hook *RedisHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.TraceLevel,
logrus.DebugLevel,
logrus.InfoLevel,
logrus.WarnLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}
func createV0Message(entry *logrus.Entry, appName, hostname string) map[string]interface{} {
m := make(map[string]interface{})
m["@timestamp"] = entry.Time.UTC().Format(time.RFC3339Nano)
m["@source_host"] = hostname
m["@message"] = entry.Message
fields := make(map[string]interface{})
fields["level"] = entry.Level.String()
fields["application"] = appName
for k, v := range entry.Data {
fields[k] = v
}
m["@fields"] = fields
return m
}
func createV1Message(entry *logrus.Entry, appName, hostname string) map[string]interface{} {
m := make(map[string]interface{})
m["@timestamp"] = entry.Time.UTC().Format(time.RFC3339Nano)
m["host"] = hostname
m["message"] = entry.Message
m["level"] = entry.Level.String()
m["application"] = appName
for k, v := range entry.Data {
m[k] = v
}
return m
}
func createAccessLogMessage(entry *logrus.Entry, appName, hostname string) map[string]interface{} {
m := make(map[string]interface{})
m["message"] = entry.Message
m["@source_host"] = hostname
fields := make(map[string]interface{})
fields["application"] = appName
for k, v := range entry.Data {
fields[k] = v
}
m["@fields"] = fields
return m
}
func newRedisConnectionPool(server, password string, port int, db int, options ...redis.DialOption) *redis.Pool {
hostPort := fmt.Sprintf("%s:%d", server, port)
return &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
dialOptions := append([]redis.DialOption{
redis.DialDatabase(db),
redis.DialPassword(password),
}, options...)
c, err := redis.Dial("tcp", hostPort, dialOptions...)
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}