go_goutils/logging/funcs_sysd_linux.go

234 lines
4.2 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"
2021-02-26 20:27:35 -05:00
2021-02-27 00:51:58 -05:00
"github.com/coreos/go-systemd/journal"
2021-02-26 20:27:35 -05:00
)
/*
2022-09-07 06:03:28 -04:00
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) {
2021-02-26 20:27:35 -05:00
// NOOP
return
2021-02-26 20:27:35 -05:00
}
/*
2022-09-07 06:03:28 -04:00
Shutdown cleanly shuts down a SystemDLogger.
err will always be nil; it's there for interface-compat.
*/
func (l *SystemDLogger) Shutdown() (err error) {
2021-02-26 20:27:35 -05:00
// NOOP
return
2021-02-26 20:27:35 -05:00
}
/*
2022-09-07 06:03:28 -04:00
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) {
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 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.
2022-01-05 05:15:38 -05:00
*/
func (l *SystemDLogger) 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 SystemDLogger.
func (l *SystemDLogger) GetDebug() (d bool) {
d = l.EnableDebug
return
}
/*
2022-09-07 06:03:28 -04:00
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) {
2021-02-26 15:52:29 -05:00
l.Prefix = prefix
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriAlert)
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriCrit)
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriDebug)
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriEmerg)
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriErr)
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriInfo)
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriNotice)
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 SystemDLogger.
2021-02-26 15:52:29 -05:00
func (l *SystemDLogger) 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
}
l.renderWrite(msg, journal.PriWarning)
return
}
2022-01-05 05:15:38 -05:00
// renderWrite prepares/formats a log message to be written to this SystemDLogger.
2021-02-26 20:27:35 -05:00
func (l *SystemDLogger) renderWrite(msg string, prio journal.Priority) {
2021-02-26 20:27:35 -05:00
// 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{
2021-02-27 00:51:58 -05:00
"DOCUMENTATION": "https://git.r00t2.io/Go_GoUtils/",
2021-02-26 20:27:35 -05:00
"SYSLOG_IDENTIFIER": l.Prefix,
2021-02-27 00:51:58 -05:00
"SYSLOG_FACILITY": "1", // USER
2021-02-26 20:27:35 -05:00
}
if err = journal.Send(msg, prio, vars); err != nil {
log.Panicln("could not send to Journald")
}
2021-02-26 15:52:29 -05:00
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
}