courgette/internal/logging/logger.go

55 lines
1.1 KiB
Go
Raw Normal View History

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
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),
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...)
}
}
func Warn(message string, args ...any) {
if len(args) == 0 {
Log.Warn.Print(message)
} else {
Log.Warn.Printf(message, args...)
}
}