~bigbes/huntsman

ref: 766fa8055977fbe223afd8a84bcf37a3d13bb1ce huntsman/internal/platform/observability/logging_test.go -rw-r--r-- 1.4 KiB
766fa805 — Eugene Blikh Add BSD 2-Clause license 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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")
	}
}