-
Notifications
You must be signed in to change notification settings - Fork 7
/
hiprus.go
91 lines (77 loc) · 1.92 KB
/
hiprus.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
// Package hiprus provides a Hipchat hook for the logrus loggin package.
package hiprus
import (
"net/url"
"github.com/Sirupsen/logrus"
"github.com/tbruyelle/hipchat-go/hipchat"
)
const (
VERSION = "2.0.0"
ColorYellow hipchat.Color = "yellow"
ColorRed hipchat.Color = "red"
ColorGreen hipchat.Color = "green"
ColorPurple hipchat.Color = "purple"
ColorGray hipchat.Color = "gray"
ColorRandom hipchat.Color = "random"
)
// HiprusHook is a logrus Hook for dispatching messages to the specified
// channel on Hipchat.
type HiprusHook struct {
// Messages with a log level not contained in this array
// will not be dispatched. If nil, all messages will be dispatched.
AcceptedLevels []logrus.Level
AuthToken string
RoomName string
// If empty, "Hiprus" will be used.
Username string
// If empty, will point to hipchat cloud
BaseURL string
c *hipchat.Client
}
func (hh *HiprusHook) Levels() []logrus.Level {
if hh.AcceptedLevels == nil {
return AllLevels
}
return hh.AcceptedLevels
}
func (hh *HiprusHook) Fire(e *logrus.Entry) error {
if hh.c == nil {
if err := hh.initClient(); err != nil {
return err
}
}
var color hipchat.Color
notify := false
switch e.Level {
case logrus.DebugLevel:
color = ColorPurple
case logrus.InfoLevel:
color = ColorGreen
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
color = ColorRed
notify = true
default:
color = ColorYellow
notify = true
}
_, err := hh.c.Room.Notification(hh.RoomName, &hipchat.NotificationRequest{
From: hh.Username,
Message: e.Message,
MessageFormat: "text",
Notify: notify,
Color: color,
})
return err
}
func (hh *HiprusHook) initClient() error {
c := hipchat.NewClient(hh.AuthToken)
if hh.BaseURL != "" {
hipchatUrl, _ := url.Parse(hh.BaseURL)
c.BaseURL = hipchatUrl
}
hh.c = c
if hh.Username == "" {
hh.Username = "HipRus"
}
return nil
}