2021-09-01 09:38:56 -05:00
|
|
|
//go:build !windows && !nacl && !plan9
|
|
|
|
// +build !windows,!nacl,!plan9
|
2015-12-14 09:16:36 -06:00
|
|
|
|
2015-11-07 19:38:27 -06:00
|
|
|
package log
|
|
|
|
|
2016-06-08 01:09:29 -05:00
|
|
|
import (
|
|
|
|
"log/syslog"
|
2016-06-16 11:21:12 -05:00
|
|
|
"os"
|
2016-06-08 01:09:29 -05:00
|
|
|
|
2022-01-06 08:28:05 -06:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
|
|
|
gokitsyslog "github.com/go-kit/log/syslog"
|
2016-06-16 11:21:12 -05:00
|
|
|
"gopkg.in/ini.v1"
|
2016-06-08 01:09:29 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type SysLogHandler struct {
|
|
|
|
syslog *syslog.Writer
|
|
|
|
Network string
|
|
|
|
Address string
|
|
|
|
Facility string
|
|
|
|
Tag string
|
2022-01-06 08:28:05 -06:00
|
|
|
Format Formatedlogger
|
|
|
|
logger log.Logger
|
2016-06-08 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
2022-01-06 08:28:05 -06:00
|
|
|
var selector = func(keyvals ...interface{}) syslog.Priority {
|
|
|
|
for i := 0; i < len(keyvals); i += 2 {
|
|
|
|
if keyvals[i] == level.Key() {
|
|
|
|
if v, ok := keyvals[i+1].(string); ok {
|
|
|
|
switch v {
|
|
|
|
case "emergency":
|
|
|
|
return syslog.LOG_EMERG
|
|
|
|
case "alert":
|
|
|
|
return syslog.LOG_ALERT
|
|
|
|
case "critical":
|
|
|
|
return syslog.LOG_CRIT
|
|
|
|
case "error":
|
|
|
|
return syslog.LOG_ERR
|
|
|
|
case "warning":
|
|
|
|
return syslog.LOG_WARNING
|
|
|
|
case "notice":
|
|
|
|
return syslog.LOG_NOTICE
|
|
|
|
case "info":
|
|
|
|
return syslog.LOG_INFO
|
|
|
|
case "debug":
|
|
|
|
return syslog.LOG_DEBUG
|
|
|
|
}
|
|
|
|
return syslog.LOG_LOCAL0
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 01:09:29 -05:00
|
|
|
}
|
2022-01-06 08:28:05 -06:00
|
|
|
return syslog.LOG_LOCAL0
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSyslog(sec *ini.Section, format Formatedlogger) *SysLogHandler {
|
|
|
|
handler := &SysLogHandler{}
|
2016-06-16 11:21:12 -05:00
|
|
|
|
|
|
|
handler.Format = format
|
|
|
|
handler.Network = sec.Key("network").MustString("")
|
|
|
|
handler.Address = sec.Key("address").MustString("")
|
|
|
|
handler.Facility = sec.Key("facility").MustString("local7")
|
|
|
|
handler.Tag = sec.Key("tag").MustString("")
|
|
|
|
|
|
|
|
if err := handler.Init(); err != nil {
|
2022-01-06 08:28:05 -06:00
|
|
|
_ = level.Error(Root).Log("Failed to init syslog log handler", "error", err)
|
2016-06-16 11:21:12 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-01-06 08:28:05 -06:00
|
|
|
handler.logger = gokitsyslog.NewSyslogLogger(handler.syslog, format, gokitsyslog.PrioritySelectorOption(selector))
|
2016-06-16 11:21:12 -05:00
|
|
|
return handler
|
2016-06-08 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *SysLogHandler) Init() error {
|
2022-01-06 08:28:05 -06:00
|
|
|
// the facility is the origin of the syslog message
|
|
|
|
prio := parseFacility(sw.Facility)
|
2016-06-08 01:09:29 -05:00
|
|
|
|
|
|
|
w, err := syslog.Dial(sw.Network, sw.Address, prio, sw.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.syslog = w
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:28:05 -06:00
|
|
|
func (sw *SysLogHandler) Log(keyvals ...interface{}) error {
|
|
|
|
err := sw.logger.Log(keyvals...)
|
2016-06-08 01:09:29 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-15 02:32:06 -06:00
|
|
|
func (sw *SysLogHandler) Close() error {
|
|
|
|
return sw.syslog.Close()
|
2016-06-08 01:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var facilities = map[string]syslog.Priority{
|
|
|
|
"user": syslog.LOG_USER,
|
|
|
|
"daemon": syslog.LOG_DAEMON,
|
|
|
|
"local0": syslog.LOG_LOCAL0,
|
|
|
|
"local1": syslog.LOG_LOCAL1,
|
|
|
|
"local2": syslog.LOG_LOCAL2,
|
|
|
|
"local3": syslog.LOG_LOCAL3,
|
|
|
|
"local4": syslog.LOG_LOCAL4,
|
|
|
|
"local5": syslog.LOG_LOCAL5,
|
|
|
|
"local6": syslog.LOG_LOCAL6,
|
|
|
|
"local7": syslog.LOG_LOCAL7,
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:28:05 -06:00
|
|
|
func parseFacility(facility string) syslog.Priority {
|
|
|
|
v, found := facilities[facility]
|
|
|
|
if !found {
|
|
|
|
// default the facility level to LOG_LOCAL7
|
|
|
|
return syslog.LOG_LOCAL7
|
2016-06-08 01:09:29 -05:00
|
|
|
}
|
2022-01-06 08:28:05 -06:00
|
|
|
return v
|
2016-06-08 01:09:29 -05:00
|
|
|
}
|