2024-08-03 17:10:01 +00:00
|
|
|
// Logging module
|
|
|
|
//
|
|
|
|
// This module is a wrapper around the built-in `log` package
|
|
|
|
// and adds more control around if and how logging shows up
|
|
|
|
// in the code.
|
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
Info log.Logger
|
|
|
|
Error log.Logger
|
2024-08-18 05:28:20 +00:00
|
|
|
Warn log.Logger
|
2024-08-03 17:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Log Logger
|
|
|
|
|
|
|
|
// Configures the loggers and initializes each logging level's instance.
|
|
|
|
//
|
|
|
|
// This should be run once and before any logging is done.
|
|
|
|
func ConfigureLogger() {
|
|
|
|
Log = Logger{
|
|
|
|
Info: *log.New(os.Stdout, "[INFO] ", log.Ldate|log.Ltime),
|
2024-08-18 05:28:20 +00:00
|
|
|
Warn: *log.New(os.Stdout, "[WARN] ", log.Ldate|log.Ltime),
|
2024-08-03 17:10:01 +00:00
|
|
|
Error: *log.New(os.Stderr, "[ERROR] ", log.Ldate|log.Ltime),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Info(message string, args ...any) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
Log.Info.Print(message)
|
|
|
|
} else {
|
|
|
|
Log.Info.Printf(message, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(message string, args ...any) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
Log.Error.Print(message)
|
|
|
|
} else {
|
|
|
|
Log.Error.Printf(message, args...)
|
|
|
|
}
|
|
|
|
}
|
2024-08-18 05:28:20 +00:00
|
|
|
|
|
|
|
func Warn(message string, args ...any) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
Log.Warn.Print(message)
|
|
|
|
} else {
|
|
|
|
Log.Warn.Printf(message, args...)
|
|
|
|
}
|
|
|
|
}
|