go_goutils/logging/funcs_sysd_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

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