go_goutils/logging/funcs_syslog_linux.go

222 lines
4.9 KiB
Go

package logging
import (
"fmt"
"log"
"log/syslog"
)
// Setup sets up/configures a SyslogLogger and prepares it for use.
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")
}
}
// Shutdown cleanly shuts down a SyslogLogger.
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)
}
}
}
// GetPrefix returns the prefix used by this SyslogLogger.
func (l *SyslogLogger) GetPrefix() (prefix string) {
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.
*/
func (l *SyslogLogger) DoDebug(d bool) {
l.EnableDebug = d
}
// SetPrefix sets the prefix for this SyslogLogger.
func (l *SyslogLogger) SetPrefix(prefix string) {
l.Prefix = prefix
l.Setup()
}
// 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
}