Add ability to change logger outputs

This commit is contained in:
IamTheFij 2020-12-10 11:56:28 -05:00
parent 496d1f5189
commit 1744abb8b4
2 changed files with 27 additions and 5 deletions

View File

@ -22,6 +22,15 @@ Also provided are a few simple methods for handling returned `error` variables,
var ( var (
// DebugLevel indicates if we should log at the debug level // DebugLevel indicates if we should log at the debug level
DebugLevel = true DebugLevel = true
// LoggerInfo is the slog Info logger
LoggerInfo = log.New(os.Stderr, "INFO ", defaultFlags)
// LoggerWarning is the slog Warning logger
LoggerWarning = log.New(os.Stderr, "WARNING ", defaultFlags)
// LoggerError is the slog Error logger
LoggerError = log.New(os.Stderr, "ERROR ", defaultFlags)
// LoggerDebug is the slog Debug logger
LoggerDebug = log.New(os.Stderr, "DEBUG ", defaultFlags)
) )
FUNCTIONS FUNCTIONS
@ -47,6 +56,9 @@ Also provided are a few simple methods for handling returned `error` variables,
func SetFlags(flag int) func SetFlags(flag int)
SetFlags allows changing the logger flags using flags found in `log` SetFlags allows changing the logger flags using flags found in `log`
func SetOutput(w io.Writer)
SetOutput allows changing the output of all loggers
func WarnOnErr(err error, format string, v ...interface{}) func WarnOnErr(err error, format string, v ...interface{})
WarnOnErr if error provided, will provide a warning if an error is provided WarnOnErr if error provided, will provide a warning if an error is provided

20
slog.go
View File

@ -4,6 +4,7 @@
package slog package slog
import ( import (
"io"
"log" "log"
"os" "os"
) )
@ -25,20 +26,29 @@ var (
LoggerError = log.New(os.Stderr, "ERROR ", defaultFlags) LoggerError = log.New(os.Stderr, "ERROR ", defaultFlags)
// LoggerDebug is the slog Debug logger // LoggerDebug is the slog Debug logger
LoggerDebug = log.New(os.Stderr, "DEBUG ", defaultFlags) LoggerDebug = log.New(os.Stderr, "DEBUG ", defaultFlags)
)
// SetFlags allows changing the logger flags using flags found in `log` allLoggers = []*log.Logger{
func SetFlags(flag int) {
for _, logger := range []*log.Logger{
LoggerInfo, LoggerInfo,
LoggerWarning, LoggerWarning,
LoggerError, LoggerError,
LoggerDebug, LoggerDebug,
} { }
)
// SetFlags allows changing the logger flags using flags found in `log`
func SetFlags(flag int) {
for _, logger := range allLoggers {
logger.SetFlags(flag) logger.SetFlags(flag)
} }
} }
// SetOutput allows changing the output of all loggers
func SetOutput(w io.Writer) {
for _, logger := range allLoggers {
logger.SetOutput(w)
}
}
// Log formats logs directly to the main logger // Log formats logs directly to the main logger
func Log(format string, v ...interface{}) { func Log(format string, v ...interface{}) {
log.Printf(format, v...) log.Printf(format, v...)