forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.go
84 lines (70 loc) · 1.74 KB
/
syslog.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
package main
import (
"fmt"
"log/syslog"
"os"
"github.com/sirupsen/logrus"
)
var (
syslogLevels = map[string]logrus.Level{
"crit": logrus.FatalLevel,
"error": logrus.ErrorLevel,
"warning": logrus.WarnLevel,
"info": logrus.InfoLevel,
}
)
type syslogHook struct {
writer *syslog.Writer
levels []logrus.Level
formatter logrus.Formatter
}
func isSyslogEnabled() (enabled bool) {
boolEnvConfig(&enabled, "IMGPROXY_SYSLOG_ENABLE")
return
}
func newSyslogHook() (*syslogHook, error) {
var (
network, addr string
level logrus.Level
tag = "imgproxy"
levelStr = "notice"
)
strEnvConfig(&network, "IMGPROXY_SYSLOG_NETWORK")
strEnvConfig(&addr, "IMGPROXY_SYSLOG_ADDRESS")
strEnvConfig(&tag, "IMGPROXY_SYSLOG_TAG")
strEnvConfig(&levelStr, "IMGPROXY_SYSLOG_LEVEL")
if l, ok := syslogLevels[levelStr]; ok {
level = l
} else {
level = logrus.InfoLevel
logWarning("Syslog level '%s' is invalid, 'info' is used", levelStr)
}
w, err := syslog.Dial(network, addr, syslog.LOG_NOTICE, tag)
return &syslogHook{
writer: w,
levels: logrus.AllLevels[:int(level)+1],
formatter: &logStructuredFormatter{},
}, err
}
func (hook *syslogHook) Fire(entry *logrus.Entry) error {
line, err := hook.formatter.Format(entry)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read entry, %v\n", err)
return err
}
switch entry.Level {
case logrus.PanicLevel, logrus.FatalLevel:
return hook.writer.Crit(string(line))
case logrus.ErrorLevel:
return hook.writer.Err(string(line))
case logrus.WarnLevel:
return hook.writer.Warning(string(line))
case logrus.InfoLevel:
return hook.writer.Info(string(line))
default:
return nil
}
}
func (hook *syslogHook) Levels() []logrus.Level {
return hook.levels
}