1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/syslog.go

85 lines
1.7 KiB
Go
Raw Normal View History

2019-01-11 17:01:48 +02:00
package main
import (
"fmt"
2019-01-11 17:01:48 +02:00
"log/syslog"
"os"
"github.com/sirupsen/logrus"
2019-01-11 17:01:48 +02:00
)
var (
syslogLevels = map[string]logrus.Level{
"crit": logrus.FatalLevel,
"error": logrus.ErrorLevel,
"warning": logrus.WarnLevel,
"info": logrus.InfoLevel,
}
2019-01-11 17:01:48 +02:00
)
type syslogHook struct {
writer *syslog.Writer
levels []logrus.Level
formatter logrus.Formatter
2019-01-11 17:01:48 +02:00
}
func isSyslogEnabled() (enabled bool) {
boolEnvConfig(&enabled, "IMGPROXY_SYSLOG_ENABLE")
return
}
2019-01-11 17:01:48 +02:00
func newSyslogHook() (*syslogHook, error) {
var (
2019-01-11 17:01:48 +02:00
network, addr string
level logrus.Level
2019-01-11 17:01:48 +02:00
tag = "imgproxy"
levelStr = "notice"
)
2019-01-11 17:01:48 +02:00
strEnvConfig(&network, "IMGPROXY_SYSLOG_NETWORK")
strEnvConfig(&addr, "IMGPROXY_SYSLOG_ADDRESS")
2019-03-28 11:27:52 +02:00
strEnvConfig(&tag, "IMGPROXY_SYSLOG_TAG")
strEnvConfig(&levelStr, "IMGPROXY_SYSLOG_LEVEL")
2019-03-28 11:27:52 +02:00
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
}
2019-01-11 17:01:48 +02:00
func (hook *syslogHook) Fire(entry *logrus.Entry) error {
line, err := hook.formatter.Format(entry)
2019-01-11 17:01:48 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read entry, %v\n", err)
return err
2019-01-11 17:01:48 +02:00
}
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
2019-01-11 17:01:48 +02:00
}
}
func (hook *syslogHook) Levels() []logrus.Level {
return hook.levels
}