playground-go/logger/logger.go

41 lines
1 KiB
Go

package logger
import (
"fmt"
"os"
"time"
)
// ANSI colors
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorYellow = "\033[33m"
colorCyan = "\033[36m"
colorWhite = "\033[97m"
)
// Log levels
const (
LevelInfo = "INFO"
LevelWarn = "WARN"
LevelError = "ERROR"
LevelFatal = "FATAL"
)
func logMessage(level, color, msg string, args ...any) {
timestamp := time.Now().Format("2006-01-02 15:04:05")
formatted := fmt.Sprintf(msg, args...)
output := fmt.Sprintf("%s[%s]%s %s[%s]%s %s\n", color, timestamp, colorReset, color, level, colorReset, formatted)
fmt.Print(output)
if level == LevelFatal {
os.Exit(1)
}
}
// Public logging functions
func Info(msg string, args ...any) { logMessage(LevelInfo, colorCyan, msg, args...) }
func Warn(msg string, args ...any) { logMessage(LevelWarn, colorYellow, msg, args...) }
func Error(msg string, args ...any) { logMessage(LevelError, colorRed, msg, args...) }
func Fatal(msg string, args ...any) { logMessage(LevelFatal, colorRed, msg, args...) }