2021-02-26 15:52:29 -05:00
|
|
|
package logging
|
|
|
|
|
2021-02-26 20:27:35 -05:00
|
|
|
import (
|
2021-02-27 00:51:58 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"log/syslog"
|
2021-02-26 20:27:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Setup() {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.alert, err = syslog.New(syslog.LOG_ALERT|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Alert")
|
|
|
|
}
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.crit, err = syslog.New(syslog.LOG_CRIT|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Crit")
|
|
|
|
}
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.debug, err = syslog.New(syslog.LOG_DEBUG|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Debug")
|
|
|
|
}
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.emerg, err = syslog.New(syslog.LOG_EMERG|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Emerg")
|
|
|
|
}
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.err, err = syslog.New(syslog.LOG_ERR|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Err")
|
|
|
|
}
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.info, err = syslog.New(syslog.LOG_INFO|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Info")
|
|
|
|
}
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.notice, err = syslog.New(syslog.LOG_NOTICE|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Notice")
|
|
|
|
}
|
2021-02-27 00:51:58 -05:00
|
|
|
if l.warning, err = syslog.New(syslog.LOG_WARNING|syslogFacility, l.Prefix); err != nil {
|
2021-02-26 20:27:35 -05:00
|
|
|
log.Panicln("could not open log for Warning")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Shutdown() {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, i := range []*syslog.Writer{l.alert, l.crit, l.debug, l.emerg, l.err, l.info, l.notice, l.warning} {
|
|
|
|
if err = i.Close(); err != nil {
|
|
|
|
log.Panicf("could not close log %#v\n", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-27 10:29:41 -04:00
|
|
|
func (l *SyslogLogger) DoDebug(d bool) {
|
2021-02-26 15:52:29 -05:00
|
|
|
l.EnableDebug = d
|
|
|
|
}
|
|
|
|
|
2021-03-27 10:29:41 -04:00
|
|
|
func (l *SyslogLogger) SetPrefix(prefix string) {
|
2021-02-26 15:52:29 -05:00
|
|
|
l.Prefix = prefix
|
2021-02-26 20:27:35 -05:00
|
|
|
l.Setup()
|
2021-02-26 15:52:29 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 04:49:03 -05:00
|
|
|
func (l *SyslogLogger) GetPrefix() (prefix string) {
|
|
|
|
|
|
|
|
prefix = l.Prefix
|
|
|
|
|
|
|
|
return
|
2021-02-27 00:51:58 -05:00
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
func (l *SyslogLogger) Alert(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.alert.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:ALERT facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Crit(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.crit.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:CRIT facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Debug(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
if !l.EnableDebug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.debug.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:DEBUG facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Emerg(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.emerg.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:EMERG facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Err(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.err.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:ERR facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Info(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.info.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:INFO facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Notice(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.notice.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:NOTICE facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *SyslogLogger) Warning(s string, v ...interface{}) (err error) {
|
2021-02-26 20:27:35 -05:00
|
|
|
var msg string
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
msg = fmt.Sprintf(s, v...)
|
|
|
|
} else {
|
|
|
|
msg = s
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = l.warning.Write([]byte(msg)); err != nil {
|
|
|
|
log.Panicf("could not write %v to syslog USER:WARNING facility/level", msg)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:52:29 -05:00
|
|
|
return
|
|
|
|
}
|