-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_test.go
More file actions
209 lines (178 loc) · 5.51 KB
/
logger_test.go
File metadata and controls
209 lines (178 loc) · 5.51 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
//
// Copyright (C) 2021 - 2025 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/logger
//
package logger
import (
"bytes"
"log/slog"
"strings"
"testing"
)
func TestStdioLogger(t *testing.T) {
b := &bytes.Buffer{}
log := slog.New(NewStdioHandler(WithWriter(b), WithLogLevel(DEBUG)))
t.Run("Debug", func(t *testing.T) {
defer b.Reset()
log.Debug("test")
txt := b.String()
if !strings.Contains(txt, "DEB") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
t.Run("Info", func(t *testing.T) {
defer b.Reset()
log.Info("test")
txt := b.String()
if !strings.Contains(txt, "INF") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
t.Run("Warn", func(t *testing.T) {
defer b.Reset()
log.Warn("test")
txt := b.String()
if !strings.Contains(txt, "WRN") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
t.Run("Error", func(t *testing.T) {
defer b.Reset()
log.Error("test")
txt := b.String()
if !strings.Contains(txt, "ERR") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
}
func TestJSONLogger(t *testing.T) {
b := &bytes.Buffer{}
log := slog.New(NewJSONHandler(WithWriter(b), WithLogLevel(DEBUG)))
t.Run("Debug", func(t *testing.T) {
defer b.Reset()
log.Debug("test")
txt := b.String()
if !strings.Contains(txt, "DEBUG") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
t.Run("Info", func(t *testing.T) {
defer b.Reset()
log.Info("test")
txt := b.String()
if !strings.Contains(txt, "INFO") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
t.Run("Warn", func(t *testing.T) {
defer b.Reset()
log.Warn("test")
txt := b.String()
if !strings.Contains(txt, "WARN") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
t.Run("Error", func(t *testing.T) {
defer b.Reset()
log.Error("test")
txt := b.String()
if !strings.Contains(txt, "ERROR") ||
!strings.Contains(txt, "test") ||
!strings.Contains(txt, "source") ||
!strings.Contains(txt, "lggr") {
t.Errorf("unexpected log line %s", txt)
}
})
}
func TestStdioLoggerWithTrie(t *testing.T) {
b := &bytes.Buffer{}
log := slog.New(NewStdioHandler(
WithWriter(b),
WithLogLevel(INFO), // Default level is INFO
WithLogLevelForMod(map[string]slog.Level{
"github.com/fogfish/logger": ERROR, // Only allow ERROR and above for this specific module
}),
))
t.Run("FilteredByModuleRule", func(t *testing.T) {
defer b.Reset()
// This should be filtered out because logger module has ERROR level requirement
log.Info("this info message should be filtered by module rule")
txt := b.String()
// Buffer should be empty since message was filtered
if txt != "" {
t.Errorf("expected empty buffer, got: %s", txt)
}
})
t.Run("AllowedByModuleRule", func(t *testing.T) {
defer b.Reset()
// This should pass through because it meets the module's ERROR requirement
log.Error("this error message should appear due to module rule")
txt := b.String()
if !strings.Contains(txt, "ERR") ||
!strings.Contains(txt, "this error message should appear due to module rule") {
t.Errorf("unexpected log line %s", txt)
}
})
t.Run("DefaultBehaviorForUnspecifiedModule", func(t *testing.T) {
defer b.Reset()
// Test needs to simulate logging from a different module path
// Since we can't easily change the source path in tests, let's create
// a logger that doesn't match any trie rules
logOther := slog.New(NewStdioHandler(
WithWriter(b),
WithLogLevel(INFO), // Default level is INFO
WithLogLevelForMod(map[string]slog.Level{
"some/other/module": ERROR, // Rule for a different module
}),
))
// This should pass through because no specific rule exists and it meets default INFO level
logOther.Info("this info message should appear due to default level")
txt := b.String()
if !strings.Contains(txt, "INF") ||
!strings.Contains(txt, "this info message should appear due to default level") {
t.Errorf("expected info message to appear with default behavior, got: %s", txt)
}
})
t.Run("FilteredByDefaultLevel", func(t *testing.T) {
defer b.Reset()
// Test with a logger that has higher default level
logStrict := slog.New(NewStdioHandler(
WithWriter(b),
WithLogLevel(ERROR), // Default level is ERROR
WithLogLevelForMod(map[string]slog.Level{
"some/other/module": DEBUG, // Rule for a different module (not this one)
}),
))
// This should be filtered out because it doesn't meet default ERROR level
logStrict.Info("this info message should be filtered by default level")
txt := b.String()
// Buffer should be empty since message was filtered by default level
if txt != "" {
t.Errorf("expected empty buffer due to default level filtering, got: %s", txt)
}
})
}