mirror of
https://github.com/PerpetualSoftware/pad.git
synced 2026-09-11 21:39:01 +00:00
e7f4448028
- Add /health/live (liveness) and /health/ready (readiness with DB check) endpoints - Add Store.Ping() for database connectivity verification - Create internal/logging package using stdlib log/slog - Support PAD_LOG_LEVEL (debug/info/warn/error) and PAD_LOG_FORMAT (text/json) env vars - Add structured request logging middleware replacing chi's default Logger - Migrate all log.Printf calls to slog with proper levels and key-value attrs - Exempt health probe endpoints from auth middleware
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
// Package logging provides structured logging using log/slog.
|
|
//
|
|
// Usage:
|
|
//
|
|
// logging.Setup("info", "json") // call once at startup
|
|
// slog.Info("something happened", "key", value)
|
|
//
|
|
// All application code should use the slog package directly after Setup has
|
|
// been called — it configures the default slog logger.
|
|
package logging
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Setup configures the default slog logger.
|
|
//
|
|
// - level: "debug", "info", "warn", "error" (default "info")
|
|
// - format: "json" or "text" (default "text")
|
|
//
|
|
// After calling Setup, use slog.Info / slog.Error / etc. everywhere.
|
|
func Setup(level, format string) {
|
|
SetupWriter(os.Stderr, level, format)
|
|
}
|
|
|
|
// SetupWriter is like Setup but writes to w instead of stderr (useful for tests).
|
|
func SetupWriter(w io.Writer, level, format string) {
|
|
lvl := parseLevel(level)
|
|
|
|
opts := &slog.HandlerOptions{
|
|
Level: lvl,
|
|
}
|
|
|
|
var handler slog.Handler
|
|
switch strings.ToLower(format) {
|
|
case "json":
|
|
handler = slog.NewJSONHandler(w, opts)
|
|
default:
|
|
handler = slog.NewTextHandler(w, opts)
|
|
}
|
|
|
|
slog.SetDefault(slog.New(handler))
|
|
}
|
|
|
|
func parseLevel(s string) slog.Level {
|
|
switch strings.ToLower(s) {
|
|
case "debug":
|
|
return slog.LevelDebug
|
|
case "warn", "warning":
|
|
return slog.LevelWarn
|
|
case "error":
|
|
return slog.LevelError
|
|
default:
|
|
return slog.LevelInfo
|
|
}
|
|
}
|