-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.go
54 lines (45 loc) · 1.19 KB
/
hook.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
package logentries
import (
"fmt"
"net"
"os"
"time"
"github.com/Sirupsen/logrus"
)
const (
format = "Jan 2 15:04:05"
)
// PapertrailHook to send logs to a logging service compatible with the Papertrail API.
type Hook struct {
Token string
udpConn net.Conn
}
// NewLogentriesHook creates a hook to be added to an instance of logger.
func NewLogentriesHook(hook *Hook) (*Hook, error) {
var err error
hook.udpConn, err = net.Dial("udp", fmt.Sprintf("data.logentries.com:10000"))
return hook, err
}
// Fire is called when a log event is fired.
func (hook *Hook) Fire(entry *logrus.Entry) error {
date := time.Now().Format(format)
msg, _ := entry.String()
payload := fmt.Sprintf("<22> %s %s : %s", date, hook.Token, msg)
bytesWritten, err := hook.udpConn.Write([]byte(payload))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to send log line to Papertrail via UDP. Wrote %d bytes before error: %v", bytesWritten, err)
return err
}
return nil
}
// Levels returns the available logging levels.
func (hook *Hook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}