package observability
import (
"log/slog"
"testing"
)
func TestNewLoggerLevels(t *testing.T) {
cases := []struct {
level string
want slog.Level
}{
{"debug", slog.LevelDebug},
{"DEBUG", slog.LevelDebug},
{"info", slog.LevelInfo},
{"warn", slog.LevelWarn},
{"error", slog.LevelError},
{"unknown", slog.LevelInfo}, // fallback
{"", slog.LevelInfo}, // fallback
}
for _, tc := range cases {
t.Run(tc.level, func(t *testing.T) {
lg := NewLogger(tc.level, "human")
if lg == nil {
t.Fatal("logger is nil")
}
// Probe whether the chosen level is enabled.
if !lg.Enabled(t.Context(), tc.want) {
t.Errorf("level %v should be enabled", tc.want)
}
// And one level below should be filtered out.
if tc.want > slog.LevelDebug {
if lg.Enabled(t.Context(), tc.want-4) {
t.Errorf("level below %v should be filtered", tc.want)
}
}
})
}
}
func TestNewLoggerFormatFallback(t *testing.T) {
// Any unrecognized format should not panic and should still return a logger.
lg := NewLogger("info", "yaml")
if lg == nil {
t.Fatal("logger is nil")
}
}
func TestNewLoggerJSONFormat(t *testing.T) {
// Just exercise the JSON path; we can't easily inspect the handler type
// without adapter, but we ensure it constructs without panic.
lg := NewLogger("info", "json")
if lg == nil {
t.Fatal("logger is nil")
}
}