2020-08-16 23:13:25 +00:00
|
|
|
// Package slog is a super simple logger that allows a few convenience methods
|
|
|
|
// for handling debug vs warning/error logs. It also adds a few conveniences for
|
|
|
|
// handling errors.
|
2020-08-16 22:03:09 +00:00
|
|
|
package slog
|
2020-08-16 21:46:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DebugLevel indicates if we should log at the debug level
|
|
|
|
DebugLevel = true
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// Default set of flags to use
|
|
|
|
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,
|
|
|
|
}
|
2020-08-16 21:46:35 +00:00
|
|
|
)
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// SetFlags allows changing the logger flags using flags found in `log`
|
|
|
|
func SetFlags(flag int) {
|
|
|
|
for _, logger := range allLoggers {
|
|
|
|
logger.SetFlags(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 23:13:25 +00:00
|
|
|
// Log formats logs directly to the main logger
|
|
|
|
func Log(format string, v ...interface{}) {
|
|
|
|
log.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// Info formats logs with an INFO prefix
|
|
|
|
func Info(format string, v ...interface{}) {
|
|
|
|
loggerInfo.Printf(format, v...)
|
2020-08-16 21:46:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// Warning will log with a WARNING prefix
|
|
|
|
func Warning(format string, v ...interface{}) {
|
2020-08-16 21:46:35 +00:00
|
|
|
loggerWarning.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// Error will log with a ERROR prefix
|
|
|
|
func Error(format string, v ...interface{}) {
|
2020-08-16 21:46:35 +00:00
|
|
|
loggerError.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// Debug will log with a DEBUG prefix if DebugLevel is set
|
|
|
|
func Debug(format string, v ...interface{}) {
|
|
|
|
if !DebugLevel {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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{}) {
|
2020-08-16 21:46:35 +00:00
|
|
|
if err != nil {
|
|
|
|
loggerWarning.Printf(format, v...)
|
|
|
|
loggerError.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// FatalOnErr if error provided, will log out details of an error and exit
|
|
|
|
func FatalOnErr(err error, format string, v ...interface{}) {
|
2020-08-16 21:46:35 +00:00
|
|
|
if err != nil {
|
|
|
|
loggerError.Printf(format, v...)
|
|
|
|
loggerError.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:34:47 +00:00
|
|
|
// PanicOnErr if error provided, will log out details of an error and exit
|
|
|
|
func PanicOnErr(err error, format string, v ...interface{}) {
|
2020-08-16 21:46:35 +00:00
|
|
|
if err != nil {
|
|
|
|
loggerError.Printf(format, v...)
|
|
|
|
loggerError.Panic(err)
|
|
|
|
}
|
|
|
|
}
|