-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlogger_test.go
More file actions
145 lines (123 loc) · 3.07 KB
/
logger_test.go
File metadata and controls
145 lines (123 loc) · 3.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
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
package ui
import (
"bytes"
"context"
"log/slog"
"strings"
"sync"
"testing"
)
func TestNopHandler_Enabled(t *testing.T) {
h := nopHandler{}
for _, level := range []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError} {
if h.Enabled(context.Background(), level) {
t.Errorf("nopHandler.Enabled(%v) = true, want false", level)
}
}
}
func TestNopHandler_Handle(t *testing.T) {
h := nopHandler{}
if err := h.Handle(context.Background(), slog.Record{}); err != nil {
t.Errorf("nopHandler.Handle() = %v, want nil", err)
}
}
func TestNopHandler_WithAttrs(t *testing.T) {
h := nopHandler{}
got := h.WithAttrs([]slog.Attr{slog.String("key", "val")})
if _, ok := got.(nopHandler); !ok {
t.Errorf("nopHandler.WithAttrs() returned %T, want nopHandler", got)
}
}
func TestNopHandler_WithGroup(t *testing.T) {
h := nopHandler{}
got := h.WithGroup("group")
if _, ok := got.(nopHandler); !ok {
t.Errorf("nopHandler.WithGroup() returned %T, want nopHandler", got)
}
}
func TestLoggerDefaultSilent(t *testing.T) {
l := Logger()
if l == nil {
t.Fatal("Logger() returned nil")
}
for _, level := range []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn} {
if l.Enabled(context.Background(), level) {
t.Errorf("default logger should not be enabled for %v", level)
}
}
}
func TestSetLogger(t *testing.T) {
orig := Logger()
t.Cleanup(func() { SetLogger(orig) })
var buf bytes.Buffer
custom := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
SetLogger(custom)
got := Logger()
if got != custom {
t.Error("Logger() did not return the custom logger set via SetLogger")
}
got.Info("test message", "key", "value")
if !strings.Contains(buf.String(), "test message") {
t.Errorf("expected log output to contain 'test message', got: %s", buf.String())
}
}
func TestSetLoggerNilRestoresSilent(t *testing.T) {
orig := Logger()
t.Cleanup(func() { SetLogger(orig) })
SetLogger(slog.Default())
SetLogger(nil)
l := Logger()
if l == nil {
t.Fatal("SetLogger(nil) should set nop logger, not nil")
}
if l.Enabled(context.Background(), slog.LevelError) {
t.Error("SetLogger(nil) should produce a disabled logger")
}
}
func TestSloggerReturnsSameAsLogger(t *testing.T) {
if slogger() != Logger() {
t.Error("slogger() and Logger() should return the same instance")
}
}
func TestLoggerConcurrentAccess(t *testing.T) {
orig := Logger()
t.Cleanup(func() { SetLogger(orig) })
var wg sync.WaitGroup
const goroutines = 100
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
l := Logger()
if l == nil {
t.Error("Logger() returned nil during concurrent access")
}
l.Debug("concurrent read")
}()
}
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
SetLogger(slog.Default())
SetLogger(nil)
}()
}
wg.Wait()
}
func BenchmarkLoggerLoad(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
l := Logger()
_ = l
}
}
func BenchmarkLoggerDisabledLog(b *testing.B) {
l := Logger()
b.ReportAllocs()
for b.Loop() {
l.Debug("message", "key", "value")
}
}