/
logger_hooks.go
138 lines (111 loc) · 2.95 KB
/
logger_hooks.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
package narada
import (
"errors"
"fmt"
"github.com/spf13/viper"
"github.com/getsentry/raven-go"
"github.com/sirupsen/logrus"
)
type LogrusSentryHook struct {
client *raven.Client
}
func NewLogrusSentryHook(client *raven.Client) LogrusSentryHook {
return LogrusSentryHook{
client: client,
}
}
func (h LogrusSentryHook) Fire(entry *logrus.Entry) error {
var notifyErr error
err, ok := entry.Data["error"].(error)
if ok {
notifyErr = err
} else {
notifyErr = errors.New(entry.Message)
}
h.client.CaptureError(notifyErr, nil)
return nil
}
func (h LogrusSentryHook) Levels() []logrus.Level {
return []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}
}
type LogrusSlackHook struct {
url string
icon string
channel string
emoji string
username string
extra map[string]interface{}
enabled bool
}
func NewLogrusSlackHook(config *viper.Viper) LogrusSlackHook {
config.SetDefault("logger.slack", false)
config.SetDefault("logger.slack_url", "")
config.SetDefault("logger.slack_icon", "")
config.SetDefault("logger.slack_emoji", ":ghost:")
config.SetDefault("logger.slack_username", config.GetString("app.name")+"_bot")
// Binding extra fields
extra := make(map[string]interface{})
extra["app_name"] = config.GetString("app.name")
extra["app_version"] = config.GetString("app.version")
return LogrusSlackHook{
url: config.GetString("logger.slack_url"),
icon: config.GetString("logger.slack_icon"),
channel: config.GetString("logger.slack_channel"),
emoji: config.GetString("logger.slack_emoji"),
username: config.GetString("logger.slack_username"),
enabled: config.GetBool("logger.slack"),
extra: config.GetStringMap("logger.slack_extra"),
}
}
func (h LogrusSlackHook) Fire(entry *logrus.Entry) error {
if !h.enabled {
return nil
}
msg := &SlackMessage{
Username: h.username,
Channel: h.channel,
IconEmoji: h.emoji,
IconUrl: h.icon,
}
data := map[string]interface{}{}
for k, v := range h.extra {
data[k] = v
}
for k, v := range entry.Data {
data[k] = v
}
newEntry := &logrus.Entry{
Logger: entry.Logger,
Data: data,
Time: entry.Time,
Level: entry.Level,
Message: entry.Message,
}
attach := NewAttachment()
if len(newEntry.Data) > 0 {
// Add a header above field data
attach.Text = "Message fields"
for k, v := range newEntry.Data {
slackField := NewField()
slackField.Title = k
slackField.Value = fmt.Sprint(v)
// If the field is <= 20 then we'll set it to short
if len(slackField.Value) <= 20 {
slackField.Short = true
}
attach.AddField(slackField)
}
attach.Pretext = newEntry.Message
} else {
attach.Text = newEntry.Message
}
attach.Fallback = newEntry.Message
attach.Color = "danger"
msg.AddAttachment(attach)
c := NewClient(h.url)
go c.SendMessage(msg)
return nil
}
func (h LogrusSlackHook) Levels() []logrus.Level {
return []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}
}