From 4addc44c0ffc9b5bb8f8164fdce19cdfcfe944f1 Mon Sep 17 00:00:00 2001 From: brent s Date: Wed, 5 Jan 2022 16:16:24 -0500 Subject: [PATCH] fixing multilogger so that v is properly nil instead of an empty slice if not specified --- logging/doc.go | 4 +++ logging/funcs_file.go | 14 +++++--- logging/funcs_multilogger.go | 64 +++++++++++++++++++++++++++++++----- logging/funcs_std.go | 6 ++-- logging/types.go | 15 +++++++-- 5 files changed, 85 insertions(+), 18 deletions(-) diff --git a/logging/doc.go b/logging/doc.go index 9b9321f..7c7150e 100644 --- a/logging/doc.go +++ b/logging/doc.go @@ -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. +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". Alert(s string, v ...interface{}) (err error) diff --git a/logging/funcs_file.go b/logging/funcs_file.go index ebd0fe1..678ba84 100644 --- a/logging/funcs_file.go +++ b/logging/funcs_file.go @@ -11,14 +11,19 @@ import ( func (l *FileLogger) Setup() { var err error + var multi io.Writer 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 { log.Panicf("could not open log file \"%v\" for writing: %v\n", l.Path, err) } // 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) } @@ -48,8 +53,9 @@ func (l *FileLogger) DoDebug(d bool) { // SetPrefix sets the prefix for this FileLogger. 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. diff --git a/logging/funcs_multilogger.go b/logging/funcs_multilogger.go index 5864b37..64c4d6d 100644 --- a/logging/funcs_multilogger.go +++ b/logging/funcs_multilogger.go @@ -99,7 +99,13 @@ func (m *MultiLogger) Alert(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } @@ -122,7 +128,13 @@ func (m *MultiLogger) Crit(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } @@ -145,7 +157,13 @@ func (m *MultiLogger) Debug(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } @@ -168,7 +186,13 @@ func (m *MultiLogger) Emerg(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } @@ -191,7 +215,13 @@ func (m *MultiLogger) Err(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } @@ -214,7 +244,13 @@ func (m *MultiLogger) Info(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } @@ -237,7 +273,13 @@ func (m *MultiLogger) Notice(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } @@ -260,7 +302,13 @@ func (m *MultiLogger) Warning(s string, v ...interface{}) (err error) { for _, l := range m.Loggers { wg.Add(1) 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) err = nil } diff --git a/logging/funcs_std.go b/logging/funcs_std.go index 319e566..bf051e2 100644 --- a/logging/funcs_std.go +++ b/logging/funcs_std.go @@ -9,7 +9,7 @@ import ( func (l *StdLogger) Setup() { l.Logger = log.Default() - l.Logger.SetPrefix(l.Prefix) + l.Logger.SetPrefix(l.Prefix + " ") } // Shutdown cleanly shuts down a StdLogger. @@ -38,8 +38,8 @@ func (l *StdLogger) DoDebug(d bool) { // SetPrefix sets the prefix for this StdLogger. func (l *StdLogger) 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 StdLogger. diff --git a/logging/types.go b/logging/types.go index 58f0c21..6abb2c7 100644 --- a/logging/types.go +++ b/logging/types.go @@ -2,7 +2,7 @@ package logging import ( "log" - "os" + `os` ) /* @@ -24,7 +24,10 @@ type Logger interface { 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 { // All log.Logger fields/methods are exposed. *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. 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 // Path is the path to the logfile. 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 *os.File }