@@ -38,6 +38,70 @@ func TestLoggerContext(t *testing.T) {
3838 }
3939}
4040
41+ func TestSetLevel (t * testing.T ) {
42+ for _ , tc := range []struct {
43+ input string
44+ expected Level
45+ wantErr bool
46+ }{
47+ // Named levels
48+ {input : "trace" , expected : TraceLevel },
49+ {input : "debug" , expected : DebugLevel },
50+ {input : "info" , expected : InfoLevel },
51+ {input : "warn" , expected : WarnLevel },
52+ {input : "warning" , expected : WarnLevel },
53+ {input : "error" , expected : ErrorLevel },
54+ {input : "fatal" , expected : FatalLevel },
55+ {input : "panic" , expected : PanicLevel },
56+ {input : "INFO" , expected : InfoLevel },
57+ {input : "DEBUG" , expected : DebugLevel },
58+
59+ // Exact numeric levels
60+ {input : "-8" , expected : TraceLevel },
61+ {input : "-4" , expected : DebugLevel },
62+ {input : "0" , expected : InfoLevel },
63+ {input : "4" , expected : WarnLevel },
64+ {input : "8" , expected : ErrorLevel },
65+ {input : "10" , expected : FatalLevel },
66+ {input : "12" , expected : PanicLevel },
67+
68+ // Nearest numeric levels (without going above)
69+ {input : "-10" , expected : TraceLevel },
70+ {input : "-6" , expected : DebugLevel },
71+ {input : "-5" , expected : DebugLevel },
72+ {input : "-2" , expected : InfoLevel },
73+ {input : "1" , expected : WarnLevel },
74+ {input : "2" , expected : WarnLevel },
75+ {input : "3" , expected : WarnLevel },
76+ {input : "6" , expected : ErrorLevel },
77+ {input : "7" , expected : ErrorLevel },
78+ {input : "9" , expected : FatalLevel },
79+ {input : "11" , expected : PanicLevel },
80+ {input : "13" , expected : PanicLevel },
81+ {input : "100" , expected : PanicLevel },
82+
83+ // Invalid
84+ {input : "bogus" , wantErr : true },
85+ {input : "" , wantErr : true },
86+ } {
87+ t .Run (tc .input , func (t * testing.T ) {
88+ err := SetLevel (tc .input )
89+ if tc .wantErr {
90+ if err == nil {
91+ t .Fatal ("expected error, got nil" )
92+ }
93+ return
94+ }
95+ if err != nil {
96+ t .Fatalf ("unexpected error: %v" , err )
97+ }
98+ if actual := GetLevel (); actual != tc .expected {
99+ t .Errorf ("expected level %v, got %v" , tc .expected , actual )
100+ }
101+ })
102+ }
103+ }
104+
41105func TestCompat (t * testing.T ) {
42106 expected := Fields {
43107 "hello1" : "world1" ,
0 commit comments