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
// 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
allLoggers = []*log.Logger{
loggerInfo,
loggerWarning,
loggerError,
loggerDebug,
}
// 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)
)
// SetFlags allows changing the logger flags using flags found in `log`
func SetFlags(flag int) {
for _, logger := range allLoggers {
for _, logger := range []*log.Logger{
LoggerInfo,
LoggerWarning,
LoggerError,
LoggerDebug,
} {
logger.SetFlags(flag)
}
}
@ -44,17 +46,17 @@ func Log(format string, v ...interface{}) {
// Info formats logs with an INFO prefix
func Info(format string, v ...interface{}) {
loggerInfo.Printf(format, v...)
LoggerInfo.Printf(format, v...)
}
// Warning will log with a WARNING prefix
func Warning(format string, v ...interface{}) {
loggerWarning.Printf(format, v...)
LoggerWarning.Printf(format, v...)
}
// Error will log with a ERROR prefix
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
@ -62,29 +64,29 @@ func Debug(format string, v ...interface{}) {
if !DebugLevel {
return
}
loggerDebug.Printf(format, v...)
LoggerDebug.Printf(format, v...)
}
// WarnOnErr if error provided, will provide a warning if an error is provided
func WarnOnErr(err error, format string, v ...interface{}) {
if err != nil {
loggerWarning.Printf(format, v...)
loggerError.Print(err)
LoggerWarning.Printf(format, v...)
LoggerError.Print(err)
}
}
// FatalOnErr if error provided, will log out details of an error and exit
func FatalOnErr(err error, format string, v ...interface{}) {
if err != nil {
loggerError.Printf(format, v...)
loggerError.Fatal(err)
LoggerError.Printf(format, v...)
LoggerError.Fatal(err)
}
}
// PanicOnErr if error provided, will log out details of an error and exit
func PanicOnErr(err error, format string, v ...interface{}) {
if err != nil {
loggerError.Printf(format, v...)
loggerError.Panic(err)
LoggerError.Printf(format, v...)
LoggerError.Panic(err)
}
}