2020-12-02 02:00:26 +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.
|
|
|
|
package slog
|
|
|
|
|
|
|
|
import (
|
2020-12-10 16:56:28 +00:00
|
|
|
"io"
|
2020-12-02 02:00:26 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DebugLevel indicates if we should log at the debug level
|
|
|
|
DebugLevel = true
|
|
|
|
|
|
|
|
// Default set of flags to use
|
|
|
|
defaultFlags = log.LstdFlags | log.Lmsgprefix
|
|
|
|
|
|
|
|
// Loggers for various levels. Prefixes are padded to align logged content
|
|
|
|
|
2020-12-09 15:42:21 +00:00
|
|
|
// 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)
|
2020-12-02 02:00:26 +00:00
|
|
|
|
2020-12-10 16:56:28 +00:00
|
|
|
allLoggers = []*log.Logger{
|
2020-12-09 15:42:21 +00:00
|
|
|
LoggerInfo,
|
|
|
|
LoggerWarning,
|
|
|
|
LoggerError,
|
|
|
|
LoggerDebug,
|
2020-12-10 16:56:28 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetFlags allows changing the logger flags using flags found in `log`
|
|
|
|
func SetFlags(flag int) {
|
|
|
|
for _, logger := range allLoggers {
|
2020-12-02 02:00:26 +00:00
|
|
|
logger.SetFlags(flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:56:28 +00:00
|
|
|
// SetOutput allows changing the output of all loggers
|
|
|
|
func SetOutput(w io.Writer) {
|
|
|
|
for _, logger := range allLoggers {
|
|
|
|
logger.SetOutput(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// Logf formats logs directly to the main logger
|
|
|
|
func Logf(format string, v ...interface{}) {
|
2020-12-02 02:00:26 +00:00
|
|
|
log.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// Debugf will log with a DEBUG prefix if DebugLevel is set
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
2020-12-11 03:09:16 +00:00
|
|
|
if !DebugLevel {
|
|
|
|
return
|
|
|
|
}
|
2021-02-17 22:57:42 +00:00
|
|
|
|
2020-12-11 03:09:16 +00:00
|
|
|
LoggerDebug.Printf(format, v...)
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// Infof formats logs with an INFO prefix
|
|
|
|
func Infof(format string, v ...interface{}) {
|
2020-12-09 15:42:21 +00:00
|
|
|
LoggerInfo.Printf(format, v...)
|
2020-12-02 02:00:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// Warningf will log with a WARNING prefix
|
|
|
|
func Warningf(format string, v ...interface{}) {
|
2020-12-09 15:42:21 +00:00
|
|
|
LoggerWarning.Printf(format, v...)
|
2020-12-02 02:00:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// Errorf will log with a ERROR prefix
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
2020-12-09 15:42:21 +00:00
|
|
|
LoggerError.Printf(format, v...)
|
2020-12-02 02:00:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// Fatalf will log with a ERROR prefix followed by exit(1)
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
2020-12-11 03:09:16 +00:00
|
|
|
LoggerError.Fatalf(format, v...)
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// Panicf will log with a ERROR prefix followed by panic()
|
|
|
|
func Panicf(format string, v ...interface{}) {
|
2020-12-11 03:09:16 +00:00
|
|
|
LoggerError.Panicf(format, v...)
|
2020-12-02 02:00:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// OnErrWarnf if error provided, will provide a warning if an error is provided
|
|
|
|
func OnErrWarnf(err error, format string, v ...interface{}) {
|
2020-12-02 02:00:26 +00:00
|
|
|
if err != nil {
|
2020-12-09 15:42:21 +00:00
|
|
|
LoggerWarning.Printf(format, v...)
|
|
|
|
LoggerError.Print(err)
|
2020-12-02 02:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// OnErrFatalf if error provided, will log out details of an error and exit
|
|
|
|
func OnErrFatalf(err error, format string, v ...interface{}) {
|
2020-12-02 02:00:26 +00:00
|
|
|
if err != nil {
|
2020-12-09 15:42:21 +00:00
|
|
|
LoggerError.Printf(format, v...)
|
|
|
|
LoggerError.Fatal(err)
|
2020-12-02 02:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:57:42 +00:00
|
|
|
// OnErrPanicf if error provided, will log out details of an error and exit
|
|
|
|
func OnErrPanicf(err error, format string, v ...interface{}) {
|
2020-12-02 02:00:26 +00:00
|
|
|
if err != nil {
|
2020-12-09 15:42:21 +00:00
|
|
|
LoggerError.Printf(format, v...)
|
|
|
|
LoggerError.Panic(err)
|
2020-12-02 02:00:26 +00:00
|
|
|
}
|
|
|
|
}
|