sojuboy/internal/logging/logging.go

25 lines
543 B
Go
Raw Normal View History

package logging
import (
"log/slog"
"os"
"strings"
)
// New returns a JSON slog logger configured to the provided level string (debug, info, warn, error).
func New(level string) *slog.Logger {
lvl := slog.LevelInfo
switch strings.ToLower(strings.TrimSpace(level)) {
case "debug":
lvl = slog.LevelDebug
case "info", "":
lvl = slog.LevelInfo
case "warn", "warning":
lvl = slog.LevelWarn
case "err", "error":
lvl = slog.LevelError
}
h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: lvl})
return slog.New(h)
}