go_goutils/logging/funcs_sysd_linux.go
brent s 39e0a1fd43
Windows Event Log, error output
Adding more Event Log support, and modifying the loggers so they return
errors in their operational functions.
2022-01-16 02:05:42 -05:00

218 lines
3.9 KiB
Go

package logging
import (
"fmt"
"log"
"github.com/coreos/go-systemd/journal"
)
/*
Setup sets up/configures a SystemDLogger and prepares it for use.
err will always be nil; it's there for interface-compat.
*/
func (l *SystemDLogger) Setup() (err error) {
// NOOP
return
}
/*
Shutdown cleanly shuts down a SystemDLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *SystemDLogger) Shutdown() (err error) {
// NOOP
return
}
/*
GetPrefix returns the prefix used by this SystemDLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *SystemDLogger) GetPrefix() (prefix string, err error) {
prefix = l.Prefix
return
}
/*
DoDebug sets the debug state of this SystemDLogger.
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 *SystemDLogger) DoDebug(d bool) (err error) {
l.EnableDebug = d
return
}
/*
SetPrefix sets the prefix for this SystemDLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *SystemDLogger) SetPrefix(prefix string) (err error) {
l.Prefix = prefix
return
}
// Alert writes an ALERT-level message to this SystemDLogger.
func (l *SystemDLogger) Alert(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, journal.PriAlert)
return
}
// Crit writes an CRITICAL-level message to this SystemDLogger.
func (l *SystemDLogger) Crit(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, journal.PriCrit)
return
}
// Debug writes a DEBUG-level message to this SystemDLogger.
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriDebug)
return
}
// Emerg writes an EMERGENCY-level message to this SystemDLogger.
func (l *SystemDLogger) Emerg(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, journal.PriEmerg)
return
}
// Err writes an ERROR-level message to this SystemDLogger.
func (l *SystemDLogger) Err(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, journal.PriErr)
return
}
// Info writes an INFO-level message to this SystemDLogger.
func (l *SystemDLogger) Info(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, journal.PriInfo)
return
}
// Notice writes a NOTICE-level message to this SystemDLogger.
func (l *SystemDLogger) Notice(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, journal.PriNotice)
return
}
// Warning writes a WARNING/WARN-level message to this SystemDLogger.
func (l *SystemDLogger) Warning(s string, v ...interface{}) (err error) {
var msg string
if v != nil {
msg = fmt.Sprintf(s, v...)
} else {
msg = s
}
l.renderWrite(msg, journal.PriWarning)
return
}
// renderWrite prepares/formats a log message to be written to this SystemDLogger.
func (l *SystemDLogger) renderWrite(msg string, prio journal.Priority) {
// TODO: implement code line, etc.
// https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
// CODE_FILE=, CODE_LINE=, CODE_FUNC=
var err error
vars := map[string]string{
"DOCUMENTATION": "https://git.r00t2.io/Go_GoUtils/",
"SYSLOG_IDENTIFIER": l.Prefix,
"SYSLOG_FACILITY": "1", // USER
}
if err = journal.Send(msg, prio, vars); err != nil {
log.Panicln("could not send to Journald")
}
return
}