Export individual loggers

This commit is contained in:
IamTheFij 2020-12-09 10:42:21 -05:00
parent f6ae8b7d5a
commit 496d1f5189

46
slog.go
View File

@ -16,23 +16,25 @@ var (
defaultFlags = log.LstdFlags | log.Lmsgprefix defaultFlags = log.LstdFlags | log.Lmsgprefix
// Loggers for various levels. Prefixes are padded to align logged content // Loggers for various levels. Prefixes are padded to align logged content
loggerInfo = log.New(os.Stderr, "INFO ", defaultFlags)
loggerWarning = log.New(os.Stderr, "WARNING ", defaultFlags)
loggerError = log.New(os.Stderr, "ERROR ", defaultFlags)
loggerDebug = log.New(os.Stderr, "DEBUG ", defaultFlags)
// Convenience for calling functions for all loggers in one method // LoggerInfo is the slog Info logger
allLoggers = []*log.Logger{ LoggerInfo = log.New(os.Stderr, "INFO ", defaultFlags)
loggerInfo, // LoggerWarning is the slog Warning logger
loggerWarning, LoggerWarning = log.New(os.Stderr, "WARNING ", defaultFlags)
loggerError, // LoggerError is the slog Error logger
loggerDebug, 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` // SetFlags allows changing the logger flags using flags found in `log`
func SetFlags(flag int) { func SetFlags(flag int) {
for _, logger := range allLoggers { for _, logger := range []*log.Logger{
LoggerInfo,
LoggerWarning,
LoggerError,
LoggerDebug,
} {
logger.SetFlags(flag) logger.SetFlags(flag)
} }
} }
@ -44,17 +46,17 @@ func Log(format string, v ...interface{}) {
// Info formats logs with an INFO prefix // Info formats logs with an INFO prefix
func Info(format string, v ...interface{}) { func Info(format string, v ...interface{}) {
loggerInfo.Printf(format, v...) LoggerInfo.Printf(format, v...)
} }
// Warning will log with a WARNING prefix // Warning will log with a WARNING prefix
func Warning(format string, v ...interface{}) { func Warning(format string, v ...interface{}) {
loggerWarning.Printf(format, v...) LoggerWarning.Printf(format, v...)
} }
// Error will log with a ERROR prefix // Error will log with a ERROR prefix
func Error(format string, v ...interface{}) { func Error(format string, v ...interface{}) {
loggerError.Printf(format, v...) LoggerError.Printf(format, v...)
} }
// Debug will log with a DEBUG prefix if DebugLevel is set // Debug will log with a DEBUG prefix if DebugLevel is set
@ -62,29 +64,29 @@ func Debug(format string, v ...interface{}) {
if !DebugLevel { if !DebugLevel {
return return
} }
loggerDebug.Printf(format, v...) LoggerDebug.Printf(format, v...)
} }
// 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
func WarnOnErr(err error, format string, v ...interface{}) { func WarnOnErr(err error, format string, v ...interface{}) {
if err != nil { if err != nil {
loggerWarning.Printf(format, v...) LoggerWarning.Printf(format, v...)
loggerError.Print(err) LoggerError.Print(err)
} }
} }
// FatalOnErr if error provided, will log out details of an error and exit // FatalOnErr if error provided, will log out details of an error and exit
func FatalOnErr(err error, format string, v ...interface{}) { func FatalOnErr(err error, format string, v ...interface{}) {
if err != nil { if err != nil {
loggerError.Printf(format, v...) LoggerError.Printf(format, v...)
loggerError.Fatal(err) LoggerError.Fatal(err)
} }
} }
// PanicOnErr if error provided, will log out details of an error and exit // PanicOnErr if error provided, will log out details of an error and exit
func PanicOnErr(err error, format string, v ...interface{}) { func PanicOnErr(err error, format string, v ...interface{}) {
if err != nil { if err != nil {
loggerError.Printf(format, v...) LoggerError.Printf(format, v...)
loggerError.Panic(err) LoggerError.Panic(err)
} }
} }