brent s
39e0a1fd43
Adding more Event Log support, and modifying the loggers so they return errors in their operational functions.
261 lines
5.3 KiB
Go
261 lines
5.3 KiB
Go
package logging
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"log/syslog"
|
|
|
|
`r00t2.io/goutils/multierr`
|
|
)
|
|
|
|
// Setup sets up/configures a SyslogLogger and prepares it for use.
|
|
func (l *SyslogLogger) Setup() (err error) {
|
|
|
|
var errs *multierr.MultiError = multierr.NewMultiError(nil)
|
|
|
|
if l.alert, err = syslog.New(syslog.LOG_ALERT|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
if l.crit, err = syslog.New(syslog.LOG_CRIT|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
if l.debug, err = syslog.New(syslog.LOG_DEBUG|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
if l.emerg, err = syslog.New(syslog.LOG_EMERG|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
if l.err, err = syslog.New(syslog.LOG_ERR|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
if l.info, err = syslog.New(syslog.LOG_INFO|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
if l.notice, err = syslog.New(syslog.LOG_NOTICE|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
if l.warning, err = syslog.New(syslog.LOG_WARNING|syslogFacility, l.Prefix); err != nil {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
|
|
if errs.Count() > 0 {
|
|
err = errs
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Shutdown cleanly shuts down a SyslogLogger.
|
|
func (l *SyslogLogger) Shutdown() (err error) {
|
|
|
|
var errs *multierr.MultiError = multierr.NewMultiError(nil)
|
|
|
|
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 {
|
|
errs.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
|
|
if errs.Count() > 0 {
|
|
err = errs
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
GetPrefix returns the prefix used by this SyslogLogger.
|
|
err will always be nil; it's there for interface-compat.
|
|
*/
|
|
func (l *SyslogLogger) GetPrefix() (prefix string, err error) {
|
|
|
|
prefix = l.Prefix
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
DoDebug sets the debug state of this SyslogLogger.
|
|
Note that this merely acts as a *safety filter* for debug messages to avoid sensitive information being written to the log.
|
|
err will always be nil; it's there for interface-compat.
|
|
*/
|
|
func (l *SyslogLogger) DoDebug(d bool) (err error) {
|
|
|
|
l.EnableDebug = d
|
|
|
|
return
|
|
}
|
|
|
|
// SetPrefix sets the prefix for this SyslogLogger.
|
|
func (l *SyslogLogger) SetPrefix(prefix string) (err error) {
|
|
|
|
l.Prefix = prefix
|
|
|
|
// We need to close the current loggers first.
|
|
if err = l.Shutdown(); err != nil {
|
|
return
|
|
}
|
|
|
|
if err = l.Setup(); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Alert writes an ALERT-level message to this SyslogLogger.
|
|
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
|
|
}
|
|
|
|
// Crit writes an CRITICAL-level message to this SyslogLogger.
|
|
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
|
|
}
|
|
|
|
// Debug writes a DEBUG-level message to this SyslogLogger.
|
|
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
|
|
}
|
|
|
|
// Emerg writes an EMERGENCY-level message to this SyslogLogger.
|
|
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
|
|
}
|
|
|
|
// Err writes an ERROR-level message to this SyslogLogger.
|
|
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
|
|
}
|
|
|
|
// Info writes an INFO-level message to this SyslogLogger.
|
|
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
|
|
}
|
|
|
|
// Notice writes a NOTICE-level message to this SyslogLogger.
|
|
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
|
|
}
|
|
|
|
// Warning writes a WARNING/WARN-level message to this SyslogLogger.
|
|
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
|
|
}
|