courgette/internal/logging/logger.go

54 lines
1.1 KiB
Go

// 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
Warn log.Logger
}
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),
Warn: *log.New(os.Stdout, "[WARN] ", log.Ldate|log.Ltime),
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...)
}
}
func Warn(message string, args ...any) {
if len(args) == 0 {
Log.Warn.Print(message)
} else {
Log.Warn.Printf(message, args...)
}
}