go_goutils/logging/funcs_sysd_linux.go

182 lines
2.6 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
)
func (l *SystemDLogger) Setup() {
// NOOP
_ = ""
}
func (l *SystemDLogger) Shutdown() {
// NOOP
_ = ""
}
func (l *SystemDLogger) DoDebug(d bool) {
2021-02-26 15:52:29 -05:00
l.EnableDebug = d
}
func (l *SystemDLogger) SetPrefix(prefix string) {
2021-02-26 15:52:29 -05:00
l.Prefix = prefix
}
func (l *SystemDLogger) GetPrefix() (prefix string) {
prefix = l.Prefix
return
2021-02-27 00:51:58 -05:00
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}