2021-08-25 16:46:29 +00:00
|
|
|
//go:build !windows && !plan9
|
2017-02-20 15:39:36 +00:00
|
|
|
// +build !windows,!plan9
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log/syslog"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SyslogHandler opens a connection to the system syslog daemon by calling
|
|
|
|
// syslog.New and writes all records to it.
|
|
|
|
func SyslogHandler(priority syslog.Priority, tag string, fmtr Format) (Handler, error) {
|
|
|
|
wr, err := syslog.New(priority, tag)
|
|
|
|
return sharedSyslog(fmtr, wr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyslogNetHandler opens a connection to a log daemon over the network and writes
|
|
|
|
// all log records to it.
|
|
|
|
func SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) (Handler, error) {
|
|
|
|
wr, err := syslog.Dial(net, addr, priority, tag)
|
|
|
|
return sharedSyslog(fmtr, wr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sharedSyslog(fmtr Format, sysWr *syslog.Writer, err error) (Handler, error) {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h := FuncHandler(func(r *Record) error {
|
|
|
|
var syslogFn = sysWr.Info
|
|
|
|
switch r.Lvl {
|
|
|
|
case LvlCrit:
|
|
|
|
syslogFn = sysWr.Crit
|
|
|
|
case LvlError:
|
|
|
|
syslogFn = sysWr.Err
|
|
|
|
case LvlWarn:
|
|
|
|
syslogFn = sysWr.Warning
|
|
|
|
case LvlInfo:
|
|
|
|
syslogFn = sysWr.Info
|
|
|
|
case LvlDebug:
|
|
|
|
syslogFn = sysWr.Debug
|
2017-02-20 16:05:15 +00:00
|
|
|
case LvlTrace:
|
|
|
|
syslogFn = func(m string) error { return nil } // There's no syslog level for trace
|
2017-02-20 15:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := strings.TrimSpace(string(fmtr.Format(r)))
|
|
|
|
return syslogFn(s)
|
|
|
|
})
|
|
|
|
return LazyHandler(&closingHandler{sysWr, h}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m muster) SyslogHandler(priority syslog.Priority, tag string, fmtr Format) Handler {
|
|
|
|
return must(SyslogHandler(priority, tag, fmtr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m muster) SyslogNetHandler(net, addr string, priority syslog.Priority, tag string, fmtr Format) Handler {
|
|
|
|
return must(SyslogNetHandler(net, addr, priority, tag, fmtr))
|
|
|
|
}
|