-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevels_test.go
More file actions
217 lines (191 loc) · 5.75 KB
/
Copy pathlevels_test.go
File metadata and controls
217 lines (191 loc) · 5.75 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package loglayer_test
import (
"errors"
"testing"
"go.loglayer.dev/v2"
)
func TestSetLevel(t *testing.T) {
log, lib := setup(t)
log.SetLevel(loglayer.LogLevelWarn)
log.Info("should be dropped")
log.Debug("should be dropped")
if lib.Len() != 0 {
t.Errorf("expected no lines below warn, got %d", lib.Len())
}
log.Warn("should appear")
log.Error("should appear")
log.Fatal("should appear")
if lib.Len() != 3 {
t.Errorf("expected 3 lines at/above warn, got %d", lib.Len())
}
}
func TestDisableLogging(t *testing.T) {
log, lib := setup(t)
log.DisableLogging()
log.Info("silenced")
log.Error("also silenced")
if lib.Len() != 0 {
t.Errorf("expected no lines after DisableLogging, got %d", lib.Len())
}
}
func TestEnableLogging(t *testing.T) {
log, lib := setup(t)
log.DisableLogging()
log.EnableLogging()
log.Info("back on")
if lib.Len() != 1 {
t.Errorf("expected 1 line after re-enable, got %d", lib.Len())
}
}
func TestDisableIndividualLevel(t *testing.T) {
log, lib := setup(t)
log.DisableLevel(loglayer.LogLevelDebug)
log.Debug("dropped")
log.Info("kept")
if lib.Len() != 1 {
t.Errorf("expected 1 line (info), got %d", lib.Len())
}
}
func TestIsLevelEnabled(t *testing.T) {
log, _ := setup(t)
log.SetLevel(loglayer.LogLevelWarn)
if log.IsLevelEnabled(loglayer.LogLevelInfo) {
t.Error("info should be disabled after SetLevel(warn)")
}
if !log.IsLevelEnabled(loglayer.LogLevelError) {
t.Error("error should be enabled after SetLevel(warn)")
}
}
func TestDisabledConfig(t *testing.T) {
log, lib := setupWithConfig(t, loglayer.Config{Disabled: true})
log.Info("should not appear")
if lib.Len() != 0 {
t.Errorf("expected no lines when Disabled=true, got %d", lib.Len())
}
}
// Trace sits below Debug. SetLevel(Debug) suppresses Trace; SetLevel(Trace)
// keeps everything.
func TestTrace_LevelOrdering(t *testing.T) {
log, lib := setup(t)
log.SetLevel(loglayer.LogLevelDebug)
log.Trace("dropped")
if lib.Len() != 0 {
t.Errorf("Trace should be suppressed by SetLevel(Debug), got %d lines", lib.Len())
}
log.SetLevel(loglayer.LogLevelTrace)
log.Trace("kept")
line := lib.PopLine()
if line == nil || line.Level != loglayer.LogLevelTrace {
t.Errorf("Trace should emit at LogLevelTrace, got %v", line)
}
}
// Trace via the builder works the same as the direct method.
func TestTrace_Builder(t *testing.T) {
log, lib := setup(t)
log.WithMetadata(loglayer.M{"k": "v"}).Trace("via builder")
line := lib.PopLine()
if line == nil || line.Level != loglayer.LogLevelTrace {
t.Fatalf("expected one Trace line, got %v", line)
}
m := line.Metadata.(loglayer.Metadata)
if m["k"] != "v" {
t.Errorf("metadata not preserved on Trace builder: %v", m)
}
}
// Panic emits then panics with the joined message string. Recover so the
// test runner doesn't crash; assert the panic value matches.
func TestPanic_EmitsThenPanics(t *testing.T) {
log, lib := setup(t)
var recovered any
func() {
defer func() { recovered = recover() }()
log.Panic("boom")
}()
if recovered == nil {
t.Fatal("Panic should have panicked, recovered nil")
}
if got, ok := recovered.(string); !ok || got != "boom" {
t.Errorf("panic value: got %v (%T), want \"boom\"", recovered, recovered)
}
line := lib.PopLine()
if line == nil || line.Level != loglayer.LogLevelPanic {
t.Errorf("expected one LogLevelPanic line, got %v", line)
}
}
// Panic via the builder works the same.
func TestPanic_Builder(t *testing.T) {
log, lib := setup(t)
var recovered any
func() {
defer func() { recovered = recover() }()
log.WithError(errors.New("test error")).Panic("crash")
}()
if recovered != "crash" {
t.Errorf("panic value: got %v, want \"crash\"", recovered)
}
line := lib.PopLine()
if line == nil || line.Level != loglayer.LogLevelPanic {
t.Fatalf("expected one panic-level line, got %v", line)
}
if line.Data["err"].(map[string]any)["message"] != "test error" {
t.Errorf("error not preserved on Panic builder: %v", line.Data)
}
}
// SetLevel(Panic) suppresses everything below Panic, including Fatal.
func TestPanic_LevelOrdering(t *testing.T) {
log, lib := setup(t)
log.SetLevel(loglayer.LogLevelPanic)
log.Fatal("dropped") // DisableFatalExit is true in setup
log.Error("dropped")
if lib.Len() != 0 {
t.Errorf("expected no lines below Panic, got %d", lib.Len())
}
func() {
defer func() { _ = recover() }()
log.Panic("kept")
}()
line := lib.PopLine()
if line == nil || line.Level != loglayer.LogLevelPanic {
t.Errorf("Panic should emit at SetLevel(Panic), got %v", line)
}
}
// IsLevelEnabled honors the new levels.
func TestTraceAndPanic_IsLevelEnabled(t *testing.T) {
log, _ := setup(t)
if !log.IsLevelEnabled(loglayer.LogLevelTrace) {
t.Error("Trace should be enabled by default")
}
if !log.IsLevelEnabled(loglayer.LogLevelPanic) {
t.Error("Panic should be enabled by default")
}
log.DisableLevel(loglayer.LogLevelTrace)
if log.IsLevelEnabled(loglayer.LogLevelTrace) {
t.Error("Trace should be disabled after DisableLevel")
}
}
func TestParseLogLevel_TraceAndPanic(t *testing.T) {
cases := []struct {
in string
want loglayer.LogLevel
wantOK bool
}{
{"trace", loglayer.LogLevelTrace, true},
{"panic", loglayer.LogLevelPanic, true},
{"info", loglayer.LogLevelInfo, true},
{"unknown", loglayer.LogLevelInfo, false},
}
for _, c := range cases {
got, ok := loglayer.ParseLogLevel(c.in)
if got != c.want || ok != c.wantOK {
t.Errorf("ParseLogLevel(%q) = (%v, %v); want (%v, %v)", c.in, got, ok, c.want, c.wantOK)
}
}
}
func TestLogLevelString_TraceAndPanic(t *testing.T) {
if loglayer.LogLevelTrace.String() != "trace" {
t.Errorf("Trace.String() = %q, want \"trace\"", loglayer.LogLevelTrace.String())
}
if loglayer.LogLevelPanic.String() != "panic" {
t.Errorf("Panic.String() = %q, want \"panic\"", loglayer.LogLevelPanic.String())
}
}