go_goutils/logging/funcs_multilogger.go
2022-01-16 06:55:29 -05:00

365 lines
8.5 KiB
Go

package logging
import (
`errors`
`fmt`
`sync`
`r00t2.io/goutils/multierr`
)
// Setup sets up/configures a MultiLogger (and all its MultiLogger.Loggers) and prepares it for use.
func (m *MultiLogger) Setup() (err error) {
var wg sync.WaitGroup
var errs *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logger Logger, lName string) {
var err2 error
defer wg.Done()
if err2 = logger.Setup(); err2 != nil {
errs.AddError(errors.New(fmt.Sprintf("error on Setup for logger %v; follows (may be out of order):", lName)))
errs.AddError(err2)
err2 = nil
}
}(l, logName)
}
wg.Wait()
if errs.Count() > 0 {
err = errs
return
}
return
}
// Shutdown cleanly shuts down a MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Shutdown() (err error) {
var wg sync.WaitGroup
var errs *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logger Logger, lName string) {
var err2 error
defer wg.Done()
if err2 = logger.Shutdown(); err2 != nil {
errs.AddError(errors.New(fmt.Sprintf("error on Shutdown for logger %v; follows (may be out of order):", lName)))
errs.AddError(err2)
err2 = nil
}
}(l, logName)
}
wg.Wait()
if errs.Count() > 0 {
err = errs
return
}
return
}
/*
GetPrefix returns the prefix used by this MultiLogger (and all its MultiLogger.Loggers).
err will always be nil; it's there for interface-compat.
*/
func (m *MultiLogger) GetPrefix() (prefix string, err error) {
prefix = m.Prefix
return
}
/*
DoDebug sets the debug state of this MultiLogger (and all its MultiLogger.Loggers).
Note that this merely acts as a *safety filter* for debug messages to avoid sensitive information being written to the log.
If you had a logger-specific EnableDebug set, you will need to re-set it to your desired state after running this method.
*/
func (m *MultiLogger) DoDebug(d bool) (err error) {
var wg sync.WaitGroup
var errs *multierr.MultiError = multierr.NewMultiError(nil)
m.EnableDebug = d
for logName, l := range m.Loggers {
wg.Add(1)
go func(logger Logger, lName string) {
var err2 error
defer wg.Done()
if err2 = l.DoDebug(d); err2 != nil {
errs.AddError(errors.New(fmt.Sprintf("error on DoDebug for logger %v; follows (may be out of order):", lName)))
errs.AddError(err2)
err2 = nil
}
}(l, logName)
}
wg.Wait()
if errs.Count() > 0 {
err = errs
return
}
return
}
/*
SetPrefix sets the prefix for this MultiLogger (and all its MultiLogger.Loggers).
If you had a logger-specific Prefix set, you will need to re-set it to your desired prefix after running this method.
*/
func (m *MultiLogger) SetPrefix(prefix string) (err error) {
var wg sync.WaitGroup
var errs *multierr.MultiError = multierr.NewMultiError(nil)
m.Prefix = prefix
for logName, l := range m.Loggers {
wg.Add(1)
go func(logger Logger, lName string) {
var err2 error
defer wg.Done()
if err2 = l.SetPrefix(prefix); err != nil {
errs.AddError(errors.New(fmt.Sprintf("error on SetPrefix for logger %v; follows (may be out of order):", lName)))
errs.AddError(err2)
err2 = nil
}
}(l, logName)
}
wg.Wait()
if errs.Count() > 0 {
err = errs
return
}
return
}
// Alert writes an ALERT-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Alert(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Alert(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Alert for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}
// Crit writes an CRITICAL-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Crit(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Crit(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Crit for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}
// Debug writes a DEBUG-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Debug(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Debug(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Debug for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}
// Emerg writes an EMERGENCY-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Emerg(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Emerg(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Emerg for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}
// Err writes an ERROR-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Err(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Err(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Err for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}
// Info writes an INFO-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Info(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Info(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Info for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}
// Notice writes a NOTICE-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Notice(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Notice(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Notice for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}
// Warning writes a WARNING/WARN-level message to this MultiLogger (and all its MultiLogger.Loggers).
func (m *MultiLogger) Warning(s string, v ...interface{}) (err error) {
var wg sync.WaitGroup
var e *multierr.MultiError = multierr.NewMultiError(nil)
for logName, l := range m.Loggers {
wg.Add(1)
go func(logObj Logger, msg, lName string, rplc ...interface{}) {
defer wg.Done()
if err = logObj.Warning(msg, rplc...); err != nil {
e.AddError(errors.New(fmt.Sprintf("error on Warning for logger %v; follows (may be out of order):", lName)))
e.AddError(err)
err = nil
}
}(l, s, logName, v...)
}
wg.Wait()
if !e.IsEmpty() {
err = e
}
return
}