119 lines
2.5 KiB
Go
119 lines
2.5 KiB
Go
package logging
|
|
|
|
import (
|
|
`os`
|
|
|
|
sysd `github.com/coreos/go-systemd/journal`
|
|
`github.com/google/uuid`
|
|
`r00t2.io/sysutils/paths`
|
|
)
|
|
|
|
/*
|
|
AddDefaultLogger adds a default Logger (as would be determined by GetLogger) to a MultiLogger.
|
|
|
|
identifier is a string to use to identify the added Logger in MultiLogger.Loggers.
|
|
If empty, one will be automatically generated.
|
|
*/
|
|
func (m *MultiLogger) AddDefaultLogger(identifier string, logPaths ...string) (err error) {
|
|
|
|
var l Logger
|
|
var exists bool
|
|
|
|
if identifier == "" {
|
|
identifier = uuid.New().String()
|
|
}
|
|
|
|
if _, exists = m.Loggers[identifier]; exists {
|
|
err = ErrExistingLogger
|
|
return
|
|
}
|
|
|
|
if l, err = GetLogger(m.EnableDebug, m.Prefix, logPaths...); err != nil {
|
|
return
|
|
}
|
|
|
|
m.Loggers[identifier] = l
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
AddSysdLogger adds a SystemDLogger to a MultiLogger.
|
|
|
|
identifier is a string to use to identify the added SystemDLogger in MultiLogger.Loggers.
|
|
If empty, one will be automatically generated.
|
|
*/
|
|
func (m *MultiLogger) AddSysdLogger(identifier string) (err error) {
|
|
|
|
var exists bool
|
|
|
|
if identifier == "" {
|
|
identifier = uuid.New().String()
|
|
}
|
|
|
|
if _, exists = m.Loggers[identifier]; exists {
|
|
err = ErrExistingLogger
|
|
return
|
|
}
|
|
|
|
if !sysd.Enabled() {
|
|
err = ErrNoSysD
|
|
return
|
|
}
|
|
|
|
m.Loggers[identifier] = &SystemDLogger{
|
|
EnableDebug: m.EnableDebug,
|
|
Prefix: m.Prefix,
|
|
}
|
|
m.Loggers[identifier].Setup()
|
|
|
|
m.Loggers[identifier].Info("logger initialized of type %T with prefix %v", m.Loggers[identifier], m.Loggers[identifier].GetPrefix())
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
AddSyslogLogger adds a SyslogLogger to a MultiLogger.
|
|
|
|
identifier is a string to use to identify the added SyslogLogger in MultiLogger.Loggers.
|
|
If empty, one will be automatically generated.
|
|
*/
|
|
func (m *MultiLogger) AddSyslogLogger(identifier string) (err error) {
|
|
|
|
var exists bool
|
|
var hasSyslog bool
|
|
var stat os.FileInfo
|
|
var devlogPath string = devlog
|
|
|
|
if identifier == "" {
|
|
identifier = uuid.New().String()
|
|
}
|
|
|
|
if _, exists = m.Loggers[identifier]; exists {
|
|
err = ErrExistingLogger
|
|
return
|
|
}
|
|
|
|
if hasSyslog, stat, err = paths.RealPathExistsStat(&devlogPath); hasSyslog && err != nil {
|
|
return
|
|
} else if !hasSyslog {
|
|
err = ErrNoSyslog
|
|
return
|
|
}
|
|
|
|
if stat.Mode().IsRegular() {
|
|
err = ErrInvalidDevLog
|
|
return
|
|
}
|
|
|
|
m.Loggers[identifier] = &SyslogLogger{
|
|
EnableDebug: m.EnableDebug,
|
|
Prefix: m.Prefix,
|
|
}
|
|
m.Loggers[identifier].Setup()
|
|
|
|
m.Loggers[identifier].Info("logger initialized of type %T with prefix %v", m.Loggers[identifier], m.Loggers[identifier].GetPrefix())
|
|
|
|
return
|
|
}
|