go_goutils/logging/funcs_syslog_linux.go
brent saner e5191383a7
v1.7.0
ADDED:
* logging.Logger objects now are able to return a stdlib *log.Logger.
2024-06-19 18:57:26 -04:00

277 lines
5.7 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
}
// GetDebug returns the debug status of this SyslogLogger.
func (l *SyslogLogger) GetDebug() (d bool) {
d = l.EnableDebug
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
}
// ToLogger returns a stdlib log.Logger.
func (l *SyslogLogger) ToLogger(prio logPrio) (stdLibLog *log.Logger) {
stdLibLog = log.New(&logWriter{backend: l, prio: prio}, "", 0)
return
}