This repository was archived by the owner on Sep 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhooks.go
More file actions
66 lines (55 loc) · 1.76 KB
/
hooks.go
File metadata and controls
66 lines (55 loc) · 1.76 KB
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
package logging
import (
"fmt"
"net"
"strconv"
"github.com/bshuster-repo/logrus-logstash-hook"
log "github.com/sirupsen/logrus"
"gopkg.in/gemnasium/logrus-graylog-hook.v2"
)
func (c LogConfig) initLogstashHook(h LogHook) error {
if err := c.validateRequiredHookSettings(h, []string{"network"}); err != nil {
return err
}
network, _ := h.Settings["network"]
conn, err := net.Dial(network, fmt.Sprintf("%s:%s", h.Settings["host"], h.Settings["port"]))
if nil != err {
log.WithError(err).WithField("hook", h.Format).Error("Failed to connect to logstash")
return ErrFailedToConfigureLogHook
}
formatter := getLogstashFormatter(h.Settings).(*logrustash.LogstashFormatter)
hook, err := logrustash.NewHookWithConn(conn, formatter.Type)
if nil != err {
log.WithError(err).WithField("hook", h.Format).Error("Failed to instantiate logstash hook")
return ErrFailedToConfigureLogHook
}
hook.TimeFormat = formatter.TimestampFormat
log.AddHook(hook)
return nil
}
func (c LogConfig) initGraylogHook(h LogHook) error {
var async bool
var err error
if asyncStr, ok := h.Settings["async"]; ok {
async, err = strconv.ParseBool(asyncStr)
if nil != err {
log.WithError(err).WithField("hook", h.Format).Error("Failed to parse async setting")
return ErrFailedToConfigureLogHook
}
}
extra := make(map[string]interface{})
for k, v := range h.Settings {
if k != "host" && k != "port" && k != "async" {
extra[k] = v
}
}
var hook *graylog.GraylogHook
if async {
hook = graylog.NewAsyncGraylogHook(fmt.Sprintf("%s:%s", h.Settings["host"], h.Settings["port"]), extra)
c.mustFlushHooks = append(c.mustFlushHooks, hook)
} else {
hook = graylog.NewGraylogHook(fmt.Sprintf("%s:%s", h.Settings["host"], h.Settings["port"]), extra)
}
log.AddHook(hook)
return nil
}