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
|
package airbrake // import "gopkg.in/gemnasium/logrus-airbrake-hook.v2"
import (
"errors"
"fmt"
"net/http"
"os"
"github.com/Sirupsen/logrus"
"gopkg.in/airbrake/gobrake.v2"
)
// AirbrakeHook to send exceptions to an exception-tracking service compatible
// with the Airbrake API.
type airbrakeHook struct {
Airbrake *gobrake.Notifier
}
func NewHook(projectID int64, apiKey, env string) *airbrakeHook {
airbrake := gobrake.NewNotifier(projectID, apiKey)
airbrake.AddFilter(func(notice *gobrake.Notice) *gobrake.Notice {
if env == "development" {
return nil
}
notice.Context["environment"] = env
return notice
})
hook := &airbrakeHook{
Airbrake: airbrake,
}
return hook
}
func (hook *airbrakeHook) Fire(entry *logrus.Entry) error {
var notifyErr error
err, ok := entry.Data["error"].(error)
if ok {
notifyErr = err
} else {
notifyErr = errors.New(entry.Message)
}
var req *http.Request
for k, v := range entry.Data {
if r, ok := v.(*http.Request); ok {
req = r
delete(entry.Data, k)
break
}
}
notice := hook.Airbrake.Notice(notifyErr, req, 3)
for k, v := range entry.Data {
notice.Context[k] = fmt.Sprintf("%s", v)
}
hook.sendNotice(notice)
return nil
}
func (hook *airbrakeHook) sendNotice(notice *gobrake.Notice) {
if _, err := hook.Airbrake.SendNotice(notice); err != nil {
fmt.Fprintf(os.Stderr, "Failed to send error to Airbrake: %v\n", err)
}
}
func (hook *airbrakeHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}
|