-
Notifications
You must be signed in to change notification settings - Fork 6
/
logruzio.go
90 lines (76 loc) · 1.85 KB
/
logruzio.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
package logruzio
import (
"io"
"net"
"github.com/sirupsen/logrus"
)
const (
endpoint = "listener.logz.io:5050"
proto = "tcp"
)
// HookOpts represents Logrus Logzio hook options
type HookOpts struct {
Conn io.Writer
Context logrus.Fields
Formatter logrus.Formatter
}
// Hook represents a Logrus Logzio hook
type Hook struct {
hookOpts HookOpts
}
// New creates a default Logzio hook.
// What it does is taking `token` and `appName` and attaching them to the log data.
// In addition, it sets a connection to the Logzio's Logstash endpoint.
// If the connection fails, it returns an error.
//
// To set more advanced configurations, initialize the hook in the following way:
//
// hook := &Hook{HookOpts{
// Conn: myConn,
// Context: logrus.Fields{...},
// Formatter: myFormatter{}
// }
func New(token string, appName string, ctx logrus.Fields) (*Hook, error) {
opts := HookOpts{Context: logrus.Fields{}}
opts.Context["token"] = token
opts.Context["type"] = appName
opts.Context["meta"] = ctx
opts.Formatter = &SimpleFormatter{}
var conn io.Writer
var err error
conn, err = net.Dial(proto, endpoint)
if err != nil {
return nil, err
}
opts.Conn = conn
return &Hook{opts}, nil
}
// Fire writes `entry` to Logzio
func (h *Hook) Fire(entry *logrus.Entry) error {
// Add in context fields.
for k, v := range h.hookOpts.Context {
// We don't override fields that are already set
if _, ok := entry.Data[k]; !ok {
entry.Data[k] = v
}
}
dataBytes, err := h.hookOpts.Formatter.Format(entry)
if err != nil {
return err
}
if _, err = h.hookOpts.Conn.Write(dataBytes); err != nil {
return err
}
return nil
}
// Levels returns logging levels
func (h *Hook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}