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 (
// DebugLevel indicates if we should log at the debug level
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
@ -47,6 +56,9 @@ Also provided are a few simple methods for handling returned `error` variables,
func SetFlags(flag int)
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{})
WarnOnErr if error provided, will provide a warning if an error is provided

20
slog.go
View File

@ -4,6 +4,7 @@
package slog
import (
"io"
"log"
"os"
)
@ -25,20 +26,29 @@ var (
LoggerError = log.New(os.Stderr, "ERROR ", defaultFlags)
// LoggerDebug is the slog Debug logger
LoggerDebug = log.New(os.Stderr, "DEBUG ", defaultFlags)
)
// SetFlags allows changing the logger flags using flags found in `log`
func SetFlags(flag int) {
for _, logger := range []*log.Logger{
allLoggers = []*log.Logger{
LoggerInfo,
LoggerWarning,
LoggerError,
LoggerDebug,
} {
}
)
// SetFlags allows changing the logger flags using flags found in `log`
func SetFlags(flag int) {
for _, logger := range allLoggers {
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
func Log(format string, v ...interface{}) {
log.Printf(format, v...)