-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptions_test.go
More file actions
75 lines (61 loc) · 2.07 KB
/
options_test.go
File metadata and controls
75 lines (61 loc) · 2.07 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
package log
import (
"context"
"sync/atomic"
"testing"
"github.com/go-coldbrew/log/loggers"
)
type countingLogger struct {
count atomic.Int64
level loggers.Level
}
func (c *countingLogger) Log(_ context.Context, _ loggers.Level, _ int, _ ...any) {
c.count.Add(1)
}
func (c *countingLogger) SetLevel(l loggers.Level) { c.level = l }
func (c *countingLogger) GetLevel() loggers.Level { return c.level }
// Tests OverrideLogLevel
func TestOverrideLogLevel(t *testing.T) {
// Set the log level to debug
ctx := context.Background()
_, found := GetOverridenLogLevel(ctx)
if found {
t.Error("Expected not to find the log level in the context")
}
ctx = OverrideLogLevel(ctx, loggers.DebugLevel)
// Get the log level from the context
level, found := GetOverridenLogLevel(ctx)
if !found {
t.Error("Expected to find the log level in the context")
}
if level != loggers.DebugLevel {
t.Error("Expected the log level to be debug")
}
}
// TestOverrideLogLevelCausesLogging verifies that a per-request override
// causes a message to be logged even when the global level would filter it.
func TestOverrideLogLevelCausesLogging(t *testing.T) {
orig := GetLogger()
t.Cleanup(func() { SetLogger(orig) })
cl := &countingLogger{level: loggers.ErrorLevel}
l := NewLogger(cl)
SetLogger(l)
ctx := context.Background()
// Without override: debug message should be filtered (global level is Error).
l.Log(ctx, loggers.DebugLevel, 0, "msg", "should be filtered")
if cl.count.Load() != 0 {
t.Error("expected debug message to be filtered at Error level")
}
// With override: debug message should pass through.
ctx = OverrideLogLevel(ctx, loggers.DebugLevel)
l.Log(ctx, loggers.DebugLevel, 0, "msg", "should pass through")
if cl.count.Load() != 1 {
t.Errorf("expected 1 log call with override, got %d", cl.count.Load())
}
// Info without override should still be filtered.
plainCtx := context.Background()
l.Log(plainCtx, loggers.InfoLevel, 0, "msg", "still filtered")
if cl.count.Load() != 1 {
t.Errorf("expected info to be filtered without override, got %d calls", cl.count.Load())
}
}