go_goutils/logging/funcs_syslog_linux.go

269 lines
5.5 KiB
Go
Raw Normal View History

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"
2022-09-07 06:03:28 -04:00
"r00t2.io/goutils/multierr"
2021-02-26 20:27:35 -05:00
)
2022-01-05 05:15:38 -05:00
// Setup sets up/configures a SyslogLogger and prepares it for use.
func (l *SyslogLogger) Setup() (err error) {
2021-02-26 20:27:35 -05:00
var errs *multierr.MultiError = multierr.NewMultiError(nil)
2021-02-26 20:27:35 -05:00
2021-02-27 00:51:58 -05:00
if l.alert, err = syslog.New(syslog.LOG_ALERT|syslogFacility, l.Prefix); err != nil {
errs.AddError(err)
err = nil
2021-02-26 20:27:35 -05:00
}
2021-02-27 00:51:58 -05:00
if l.crit, err = syslog.New(syslog.LOG_CRIT|syslogFacility, l.Prefix); err != nil {
errs.AddError(err)
err = nil
2021-02-26 20:27:35 -05:00
}
2021-02-27 00:51:58 -05:00
if l.debug, err = syslog.New(syslog.LOG_DEBUG|syslogFacility, l.Prefix); err != nil {
errs.AddError(err)
err = nil
2021-02-26 20:27:35 -05:00
}
2021-02-27 00:51:58 -05:00
if l.emerg, err = syslog.New(syslog.LOG_EMERG|syslogFacility, l.Prefix); err != nil {
errs.AddError(err)
err = nil
2021-02-26 20:27:35 -05:00
}
2021-02-27 00:51:58 -05:00
if l.err, err = syslog.New(syslog.LOG_ERR|syslogFacility, l.Prefix); err != nil {
errs.AddError(err)
err = nil
2021-02-26 20:27:35 -05:00
}
2021-02-27 00:51:58 -05:00
if l.info, err = syslog.New(syslog.LOG_INFO|syslogFacility, l.Prefix); err != nil {
errs.AddError(err)
err = nil
2021-02-26 20:27:35 -05:00
}
2021-02-27 00:51:58 -05:00
if l.notice, err = syslog.New(syslog.LOG_NOTICE|syslogFacility, l.Prefix); err != nil {
errs.AddError(err)
err = nil
2021-02-26 20:27:35 -05:00
}
2021-02-27 00:51:58 -05:00
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
2021-02-26 20:27:35 -05:00
}
return
2021-02-26 20:27:35 -05:00
}
2022-01-05 05:15:38 -05:00
// Shutdown cleanly shuts down a SyslogLogger.
func (l *SyslogLogger) Shutdown() (err error) {
2021-02-26 20:27:35 -05:00
var errs *multierr.MultiError = multierr.NewMultiError(nil)
2021-02-26 20:27:35 -05:00
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
2021-02-26 20:27:35 -05:00
}
}
if errs.Count() > 0 {
err = errs
}
return
2021-02-26 20:27:35 -05:00
}
/*
2022-09-07 06:03:28 -04:00
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) {
2022-01-05 05:15:38 -05:00
prefix = l.Prefix
return
}
/*
2022-09-07 06:03:28 -04:00
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.
2022-01-05 05:15:38 -05:00
*/
func (l *SyslogLogger) DoDebug(d bool) (err error) {
2021-02-26 15:52:29 -05:00
l.EnableDebug = d
return
2021-02-26 15:52:29 -05:00
}
2022-09-07 06:03:28 -04:00
// GetDebug returns the debug status of this SyslogLogger.
func (l *SyslogLogger) GetDebug() (d bool) {
d = l.EnableDebug
return
}
2022-01-05 05:15:38 -05:00
// SetPrefix sets the prefix for this SyslogLogger.
func (l *SyslogLogger) SetPrefix(prefix string) (err error) {
2021-02-26 15:52:29 -05:00
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
2021-02-26 15:52:29 -05:00
}
2022-01-05 05:15:38 -05:00
// Alert writes an ALERT-level message to this SyslogLogger.
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
}
2022-01-05 05:15:38 -05:00
// Crit writes an CRITICAL-level message to this SyslogLogger.
2021-02-26 15:52:29 -05:00
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
}
2022-01-05 05:15:38 -05:00
// Debug writes a DEBUG-level message to this SyslogLogger.
2021-02-26 15:52:29 -05:00
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
}
2022-01-05 05:15:38 -05:00
// Emerg writes an EMERGENCY-level message to this SyslogLogger.
2021-02-26 15:52:29 -05:00
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
}
2022-01-05 05:15:38 -05:00
// Err writes an ERROR-level message to this SyslogLogger.
2021-02-26 15:52:29 -05:00
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
}
2022-01-05 05:15:38 -05:00
// Info writes an INFO-level message to this SyslogLogger.
2021-02-26 15:52:29 -05:00
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
}
2022-01-05 05:15:38 -05:00
// Notice writes a NOTICE-level message to this SyslogLogger.
2021-02-26 15:52:29 -05:00
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
}
2022-01-05 05:15:38 -05:00
// Warning writes a WARNING/WARN-level message to this SyslogLogger.
2021-02-26 15:52:29 -05:00
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
}