go_goutils/logging/funcs_syslog.go
2021-02-26 20:27:35 -05:00

199 lines
4.0 KiB
Go

package logging
import (
`fmt`
`log`
`log/syslog`
)
func (l *SyslogLogger) Setup() {
var err error
if l.alert, err = syslog.New(syslog.LOG_ALERT | syslogFacility, l.Prefix); err != nil {
log.Panicln("could not open log for Alert")
}
if l.crit, err = syslog.New(syslog.LOG_CRIT | syslogFacility, l.Prefix); err != nil {
log.Panicln("could not open log for Crit")
}
if l.debug, err = syslog.New(syslog.LOG_DEBUG | syslogFacility, l.Prefix); err != nil {
log.Panicln("could not open log for Debug")
}
if l.emerg, err = syslog.New(syslog.LOG_EMERG | syslogFacility, l.Prefix); err != nil {
log.Panicln("could not open log for Emerg")
}
if l.err, err = syslog.New(syslog.LOG_ERR | syslogFacility, l.Prefix); err != nil {
log.Panicln("could not open log for Err")
}
if l.info, err = syslog.New(syslog.LOG_INFO | syslogFacility, l.Prefix); err != nil {
log.Panicln("could not open log for Info")
}
if l.notice, err = syslog.New(syslog.LOG_NOTICE | syslogFacility, l.Prefix); err != nil {
log.Panicln("could not open log for Notice")
}
if l.warning, err = syslog.New(syslog.LOG_WARNING | syslogFacility, l.Prefix); err != nil {
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)
}
}
}
func (l *SyslogLogger) doDebug(d bool) {
l.EnableDebug = d
}
func (l *SyslogLogger) setPrefix(prefix string) {
l.Prefix = prefix
l.Setup()
}
func (l *SyslogLogger) Alert(s string, v ...interface{}) (err error) {
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)
}
return
}
func (l *SyslogLogger) Crit(s string, v ...interface{}) (err error) {
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)
}
return
}
func (l *SyslogLogger) Debug(s string, v ...interface{}) (err error) {
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)
}
return
}
func (l *SyslogLogger) Emerg(s string, v ...interface{}) (err error) {
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)
}
return
}
func (l *SyslogLogger) Err(s string, v ...interface{}) (err error) {
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)
}
return
}
func (l *SyslogLogger) Info(s string, v ...interface{}) (err error) {
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)
}
return
}
func (l *SyslogLogger) Notice(s string, v ...interface{}) (err error) {
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)
}
return
}
func (l *SyslogLogger) Warning(s string, v ...interface{}) (err error) {
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)
}
return
}