-
Notifications
You must be signed in to change notification settings - Fork 3
/
slacklogrus.go
150 lines (133 loc) · 3.24 KB
/
slacklogrus.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
// Package slacklogrus provides a hook for Slack and the Logrus package.
//
// Installation
// go get -u github.com/codehakase/slack-logrus
//
//
// Usage
// package main
// import (
// "github.com/codehakase/slack-logrus"
// "github.com/sirupsen/logrus"
// )
// func main() {
// logrus.SetOutput(os.Stderr)
// logrus.AddHook(&sl.Hook{
// SlackHookURL: "https://hooks.slack.com/services/xxxxxx/xxxxxx/xxxxxxx",
// Username: "slack-logrus",
// IconEmoji: ":mega:",
// WithLevels: logrus.AllLevels,
// Channel: "#dev-channel",
// })
// logrus.Info("Hey I'm test running my package!!!")
// }
//
// // To send custom fields to aid understand logging use add an `Options` field
// fields := map[string]interface{}{
// "hostname": "avivacore",
// "source": os.Getenv("APISource"),
// "tag": "test-tag",
// }
// logrus.AddHook(&slacklogrus.Hook{
// ...
// Options: fields,
// })
package slacklogrus
import (
"fmt"
"github.com/multiplay/go-slack/chat"
"github.com/multiplay/go-slack/webhook"
"github.com/sirupsen/logrus"
)
// Hook is a logrus Hook for dispatching messages to a defined slack channel
type Hook struct {
WithLevels []logrus.Level
SlackHookURL string
Channel string
IconEmoji string
IconURL string
Username string
Options map[string]interface{}
}
// Implement Hook methods
// Fire creates a new event, and sends to Slack
func (h *Hook) Fire(entry *logrus.Entry) error {
var wlvl_match bool
for _, wlvl := range h.WithLevels {
if wlvl == entry.Level {
wlvl_match = true
break
}
}
// entry is not of desired log level
if !wlvl_match {
return nil
}
var color string
switch entry.Level {
case logrus.InfoLevel:
color = "good"
case logrus.DebugLevel:
color = "#9B30FF"
case logrus.FatalLevel, logrus.PanicLevel, logrus.ErrorLevel:
color = "danger"
default:
color = "warning"
}
// setup slack connection
c := webhook.New(h.SlackHookURL)
text := &chat.Message{
Username: h.Username,
Channel: h.Channel,
IconEmoji: h.IconEmoji,
IconURL: h.IconURL,
}
// add log data as slack attachment
atx := text.NewAttachment()
// fetch all entries and add as attachment
allEntries := h.newEntry(entry)
if len(allEntries.Data) > 0 {
atx.Text = "Log fields"
for k, v := range allEntries.Data {
field := &chat.Field{}
field.Title = k
field.Value = fmt.Sprint(v)
atx.AddField(field)
}
atx.PreText = allEntries.Message
} else {
atx.Text = allEntries.Message
}
atx.Color = color
// Execute concurrently
go text.Send(c)
return nil
}
// newEntry adds a new entry to the Logger
func (h *Hook) newEntry(entry *logrus.Entry) *logrus.Entry {
data := map[string]interface{}{}
for k, v := range h.Options {
data[k] = v
}
for k, v := range entry.Data {
data[k] = v
}
return &logrus.Entry{
Logger: entry.Logger,
Time: entry.Time,
Level: entry.Level,
Data: data,
Message: entry.Message,
}
}
// SetLevels specifies neccessary levels for a particular hook
func (h *Hook) SetLevels(level []logrus.Level) {
h.WithLevels = level
}
// Levels sent to slack
func (h *Hook) Levels() []logrus.Level {
if h.WithLevels == nil {
return logrus.AllLevels
}
return h.WithLevels
}