fixing multilogger so that v is properly nil instead of an empty slice if not specified

This commit is contained in:
brent s. 2022-01-05 16:16:24 -05:00
parent 0e01306637
commit 4addc44c0f
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
5 changed files with 85 additions and 18 deletions

View File

@ -11,6 +11,10 @@ These particular loggers (logging.Logger) available are:


There is a sixth type of logging.Logger, MultiLogger, that allows for multiple loggers to be written to with a single call. There is a sixth type of logging.Logger, MultiLogger, that allows for multiple loggers to be written to with a single call.


Note that for some Loggers, the prefix may be modified - "literal" loggers (StdLogger and FileLogger) will append a space to the end of the prefix.
If this is undesired (unlikely), you will need to modify (Logger).Prefix and run (Logger).Logger.SetPrefix(yourPrefixHere) for the respective logger.


Every logging.Logger type has the following methods that correspond to certain "levels". Every logging.Logger type has the following methods that correspond to certain "levels".


Alert(s string, v ...interface{}) (err error) Alert(s string, v ...interface{}) (err error)

View File

@ -11,14 +11,19 @@ import (
func (l *FileLogger) Setup() { func (l *FileLogger) Setup() {


var err error var err error
var multi io.Writer


l.Logger = log.Default() l.Logger = log.Default()
l.Logger.SetPrefix(l.Prefix) l.Logger.SetPrefix(l.Prefix + " ")
if l.writer, err = os.OpenFile(l.Path, appendFlags, logPerm); err != nil { if l.writer, err = os.OpenFile(l.Path, appendFlags, logPerm); err != nil {
log.Panicf("could not open log file \"%v\" for writing: %v\n", l.Path, err) log.Panicf("could not open log file \"%v\" for writing: %v\n", l.Path, err)
} }
// https://stackoverflow.com/a/36719588/733214 // https://stackoverflow.com/a/36719588/733214
multi := io.MultiWriter(os.Stdout, l.writer) if l.EnableStdOut {
multi = io.MultiWriter(os.Stdout, l.writer)
} else {
multi = l.writer
}
l.Logger.SetOutput(multi) l.Logger.SetOutput(multi)
} }


@ -48,8 +53,9 @@ func (l *FileLogger) DoDebug(d bool) {


// SetPrefix sets the prefix for this FileLogger. // SetPrefix sets the prefix for this FileLogger.
func (l *FileLogger) SetPrefix(prefix string) { func (l *FileLogger) SetPrefix(prefix string) {
l.Prefix = prefix
l.Logger.SetPrefix(prefix) l.Prefix = prefix + " "
l.Logger.SetPrefix(prefix + " ")
} }


// Alert writes an ALERT-level message to this FileLogger. // Alert writes an ALERT-level message to this FileLogger.

View File

@ -99,7 +99,13 @@ func (m *MultiLogger) Alert(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Alert(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Alert(msg, rplc...)
} else {
err = logObj.Alert(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }
@ -122,7 +128,13 @@ func (m *MultiLogger) Crit(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Crit(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Crit(msg, rplc...)
} else {
err = logObj.Crit(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }
@ -145,7 +157,13 @@ func (m *MultiLogger) Debug(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Debug(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Debug(msg, rplc...)
} else {
err = logObj.Debug(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }
@ -168,7 +186,13 @@ func (m *MultiLogger) Emerg(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Emerg(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Emerg(msg, rplc...)
} else {
err = logObj.Emerg(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }
@ -191,7 +215,13 @@ func (m *MultiLogger) Err(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Err(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Err(msg, rplc...)
} else {
err = logObj.Err(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }
@ -214,7 +244,13 @@ func (m *MultiLogger) Info(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Info(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Info(msg, rplc...)
} else {
err = logObj.Info(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }
@ -237,7 +273,13 @@ func (m *MultiLogger) Notice(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Notice(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Notice(msg, rplc...)
} else {
err = logObj.Notice(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }
@ -260,7 +302,13 @@ func (m *MultiLogger) Warning(s string, v ...interface{}) (err error) {
for _, l := range m.Loggers { for _, l := range m.Loggers {
wg.Add(1) wg.Add(1)
go func(logObj Logger, msg string, rplc ...interface{}) { go func(logObj Logger, msg string, rplc ...interface{}) {
if err = logObj.Warning(msg, rplc...); err != nil { defer wg.Done()
if rplc != nil {
err = logObj.Warning(msg, rplc...)
} else {
err = logObj.Warning(msg)
}
if err != nil {
e.AddError(err) e.AddError(err)
err = nil err = nil
} }

View File

@ -9,7 +9,7 @@ import (
func (l *StdLogger) Setup() { func (l *StdLogger) Setup() {


l.Logger = log.Default() l.Logger = log.Default()
l.Logger.SetPrefix(l.Prefix) l.Logger.SetPrefix(l.Prefix + " ")
} }


// Shutdown cleanly shuts down a StdLogger. // Shutdown cleanly shuts down a StdLogger.
@ -38,8 +38,8 @@ func (l *StdLogger) DoDebug(d bool) {


// SetPrefix sets the prefix for this StdLogger. // SetPrefix sets the prefix for this StdLogger.
func (l *StdLogger) SetPrefix(prefix string) { func (l *StdLogger) SetPrefix(prefix string) {
l.Prefix = prefix l.Prefix = prefix + " "
l.Logger.SetPrefix(prefix) l.Logger.SetPrefix(prefix + " ")
} }


// Alert writes an ALERT-level message to this StdLogger. // Alert writes an ALERT-level message to this StdLogger.

View File

@ -2,7 +2,7 @@ package logging


import ( import (
"log" "log"
"os" `os`
) )


/* /*
@ -24,7 +24,10 @@ type Logger interface {
Shutdown() Shutdown()
} }


// StdLogger uses the log package in stdlib to perform all logging. /*
StdLogger uses the log package in stdlib to perform all logging. The default is to write to STDOUT.
If you wish to modify the underling log.Logger object, you can access it directly via StdLogger.Logger.
*/
type StdLogger struct { type StdLogger struct {
// All log.Logger fields/methods are exposed. // All log.Logger fields/methods are exposed.
*log.Logger *log.Logger
@ -39,10 +42,16 @@ type StdLogger struct {


// FileLogger uses a StdLogger with a file handle writer to write to the file given at Path. // FileLogger uses a StdLogger with a file handle writer to write to the file given at Path.
type FileLogger struct { type FileLogger struct {
// StdLogger is used for the log formation and handling. // StdLogger is used for the log formation and handling. See StdLogger for more details.
StdLogger StdLogger
// Path is the path to the logfile. // Path is the path to the logfile.
Path string Path string
/*
EnableStdOut is true if the log will send to STDOUT as well as the file.
If false (default), it will only (silently) write to the log file.
You will need to run *FileLogger.Shutdown and then *FileLogger.Setup again if you wish to change this.
*/
EnableStdOut bool
// writer is used for the writing out of the log file. // writer is used for the writing out of the log file.
writer *os.File writer *os.File
} }