/
graylog.go
50 lines (39 loc) · 944 Bytes
/
graylog.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
package hooks
import (
"os"
"github.com/rai-project/config"
"github.com/rai-project/logger"
"github.com/rai-project/utils"
"github.com/spf13/viper"
"gopkg.in/gemnasium/logrus-graylog-hook.v2"
)
func init() {
config.OnInit(func() {
config.App.Wait()
logger.Config.Wait()
if !logger.UsingHook("graylog") {
return
}
address := decrypt(viper.GetString("graylog.address"))
if address == "" {
return
}
port := decrypt(viper.GetString("graylog.port"))
if port == "" {
port = "12201"
}
ctx := map[string]interface{}{
"ID": config.App.Name,
"Version": config.App.Version.Version,
"BuildDate": config.App.Version.BuildDate,
}
if ip, err := utils.GetExternalIp(); err == nil {
ctx["IP"] = ip
}
if hostname, err := os.Hostname(); err == nil {
ctx["HostName"] = hostname
}
hook := graylog.NewGraylogHook(address+":"+port, ctx)
logger.RegisterHook("graylog", hook)
})
}