-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_test.go
More file actions
256 lines (213 loc) · 6.02 KB
/
logger_test.go
File metadata and controls
256 lines (213 loc) · 6.02 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package disk
import (
"bytes"
"os"
"strings"
"testing"
"time"
)
func TestLogger(t *testing.T) {
t.Run("Logger levels work correctly", func(t *testing.T) {
var buf bytes.Buffer
config := &LoggerConfig{
Level: WARN,
Output: &buf,
Prefix: "[test] ",
Structured: true,
}
logger := NewLogger(config)
// These should not appear in output
logger.Debug("debug message")
logger.Info("info message")
// These should appear
logger.Warn("warn message")
logger.Error("error message")
output := buf.String()
if strings.Contains(output, "debug message") {
t.Error("Debug message should not appear with WARN level")
}
if strings.Contains(output, "info message") {
t.Error("Info message should not appear with WARN level")
}
if !strings.Contains(output, "warn message") {
t.Error("Warn message should appear with WARN level")
}
if !strings.Contains(output, "error message") {
t.Error("Error message should appear with WARN level")
}
})
t.Run("Verbose mode enables DEBUG level", func(t *testing.T) {
var buf bytes.Buffer
config := &LoggerConfig{
Level: INFO,
Output: &buf,
Verbose: true,
}
logger := NewLogger(config)
logger.Debug("debug message")
output := buf.String()
if !strings.Contains(output, "debug message") {
t.Error("Debug message should appear in verbose mode")
}
})
t.Run("Sanitization works correctly", func(t *testing.T) {
var buf bytes.Buffer
config := &LoggerConfig{
Level: DEBUG,
Output: &buf,
SanitizeAuth: true,
}
logger := NewLogger(config)
// Test various sensitive keys
sanitized := logger.SanitizeValue("Authorization", "Bearer very-secret-token-here")
if !strings.Contains(sanitized, "***") {
t.Error("Authorization header should be sanitized")
}
sanitized = logger.SanitizeValue("Content-Type", "application/json")
if strings.Contains(sanitized, "***") {
t.Error("Content-Type header should not be sanitized")
}
// Test short tokens
sanitized = logger.SanitizeValue("token", "short")
if sanitized != "***" {
t.Error("Short tokens should be completely hidden")
}
})
t.Run("Silent mode logs nothing", func(t *testing.T) {
var buf bytes.Buffer
config := &LoggerConfig{
Level: SILENT,
Output: &buf,
}
logger := NewLogger(config)
logger.Error("error message")
if buf.Len() > 0 {
t.Error("Silent mode should log nothing")
}
})
}
func TestClientLogging(t *testing.T) {
t.Run("Client with custom logging config", func(t *testing.T) {
var buf bytes.Buffer
config := &ClientConfig{
DefaultTimeout: 30 * time.Second,
Logger: &LoggerConfig{
Level: DEBUG,
Output: &buf,
Verbose: true,
},
}
client, err := NewWithConfig(config, "test-token")
if err != nil {
t.Fatal("Failed to create client:", err)
}
if client.Logger == nil {
t.Fatal("Client logger should not be nil")
}
// Test log level setting
client.SetLogLevel(ERROR)
client.Logger.Info("info message")
client.Logger.Error("error message")
output := buf.String()
if strings.Contains(output, "info message") {
t.Error("Info message should not appear with ERROR level")
}
if !strings.Contains(output, "error message") {
t.Error("Error message should appear with ERROR level")
}
})
t.Run("Log output can be changed", func(t *testing.T) {
client, _ := New("test-token")
var buf bytes.Buffer
client.SetLogOutput(&buf)
client.Logger.Info("test message")
if !strings.Contains(buf.String(), "test message") {
t.Error("Message should appear in custom output")
}
})
t.Run("Verbose mode can be toggled", func(t *testing.T) {
var buf bytes.Buffer
client, _ := New("test-token")
client.SetLogOutput(&buf)
client.SetVerbose(true)
client.Logger.Debug("debug message")
if !strings.Contains(buf.String(), "debug message") {
t.Error("Debug message should appear in verbose mode")
}
})
}
func TestRequestResponseLogging(t *testing.T) {
t.Run("Logger sanitizes authorization headers", func(t *testing.T) {
var buf bytes.Buffer
config := &LoggerConfig{
Level: DEBUG,
Output: &buf,
Verbose: true,
SanitizeAuth: true,
}
logger := NewLogger(config)
// Test that logger sanitizes headers correctly
headers := map[string]string{
"Authorization": "OAuth very-secret-token-here",
"Content-Type": "application/json",
}
logger.LogRequest("GET", "https://example.com", headers)
output := buf.String()
if !strings.Contains(output, "HTTP Request") {
t.Error("Should log HTTP request")
}
// In verbose mode, headers should be logged
if strings.Contains(output, "very-secret-token-here") {
t.Error("Full token should not appear in logs")
}
if !strings.Contains(output, "***") {
t.Error("Should contain sanitized authorization")
}
})
t.Run("Response logging works correctly", func(t *testing.T) {
var buf bytes.Buffer
config := &LoggerConfig{
Level: DEBUG,
Output: &buf,
}
logger := NewLogger(config)
logger.LogResponse(200, 1024, 150*time.Millisecond)
output := buf.String()
if !strings.Contains(output, "HTTP Response") {
t.Error("Should log HTTP response")
}
if !strings.Contains(output, "200") {
t.Error("Should include status code")
}
if !strings.Contains(output, "1024") {
t.Error("Should include content length")
}
})
}
func TestFileLogging(t *testing.T) {
t.Run("Can log to file", func(t *testing.T) {
// Create a temporary file for testing
tmpFile, err := os.CreateTemp("", "disklog_test_*.log")
if err != nil {
t.Fatal("Failed to create temp file:", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
config := &ClientConfig{
Logger: &LoggerConfig{
Level: INFO,
Output: tmpFile,
},
}
client, _ := NewWithConfig(config, "test-token")
client.Logger.Info("test file logging")
// Read file contents
tmpFile.Seek(0, 0)
buf := make([]byte, 1024)
n, _ := tmpFile.Read(buf)
content := string(buf[:n])
if !strings.Contains(content, "test file logging") {
t.Error("Log should be written to file")
}
})
}