-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_test.go
More file actions
492 lines (402 loc) · 12.5 KB
/
Copy pathlogger_test.go
File metadata and controls
492 lines (402 loc) · 12.5 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package iSlogger
import (
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestNew(t *testing.T) {
config := DefaultConfig().
WithAppName("test").
WithLogDir("test-logs").
WithLogLevel(slog.LevelDebug)
logger, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("test-logs")
if logger.config.AppName != "test" {
t.Errorf("Expected app name 'test', got '%s'", logger.config.AppName)
}
if logger.config.LogLevel != slog.LevelDebug {
t.Error("Expected log level to be DEBUG")
}
}
func TestLogLevels(t *testing.T) {
config := DefaultConfig().
WithAppName("test-levels").
WithLogDir("test-logs-levels").
WithLogLevel(slog.LevelDebug)
logger, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("test-logs-levels")
logger.Debug("Debug message", "key", "value")
logger.Info("Info message", "key", "value")
logger.Warn("Warning message", "key", "value")
logger.Error("Error message", "key", "value")
today := time.Now().Format("2006-01-02")
infoPath := filepath.Join("test-logs-levels", "test-levels_"+today+".log")
errorPath := filepath.Join("test-logs-levels", "test-levels_error_"+today+".log")
if _, err := os.Stat(infoPath); os.IsNotExist(err) {
t.Error("Info log file was not created")
}
if _, err := os.Stat(errorPath); os.IsNotExist(err) {
t.Error("Error log file was not created")
}
}
func TestLogLevelChange(t *testing.T) {
config := DefaultConfig().
WithAppName("test-level").
WithLogDir("test-logs-level").
WithLogLevel(slog.LevelWarn) // Start with WARN level
logger, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("test-logs-level")
if logger.config.LogLevel != slog.LevelWarn {
t.Error("Expected log level to be WARN initially")
}
err = logger.SetLevel(slog.LevelDebug)
if err != nil {
t.Errorf("Failed to change log level: %v", err)
}
if logger.config.LogLevel != slog.LevelDebug {
t.Error("Expected log level to be DEBUG after SetLevel(DEBUG)")
}
}
func TestWith(t *testing.T) {
config := DefaultConfig().
WithAppName("test-with").
WithLogDir("test-logs-with").
WithLogLevel(slog.LevelDebug)
logger, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("test-logs-with")
contextLogger := logger.With("user_id", 123, "session", "abc")
contextLogger.Info("Test message with context")
logger.Info("Original logger message")
}
func TestGlobalLogger(t *testing.T) {
config := DefaultConfig().
WithAppName("test-global").
WithLogDir("test-logs-global").
WithLogLevel(slog.LevelDebug)
err := Init(config)
if err != nil {
t.Fatalf("Failed to initialize global logger: %v", err)
}
defer Close()
defer os.RemoveAll("test-logs-global")
Debug("Global debug message")
Info("Global info message")
Warn("Global warning message")
Error("Global error message")
contextLogger := With("global_key", "global_value")
if contextLogger == nil {
t.Error("Expected non-nil logger from global With()")
}
}
func TestConfigBuilder(t *testing.T) {
config := DefaultConfig().
WithAppName("builder-test").
WithLogDir("builder-logs").
WithLogLevel(slog.LevelDebug).
WithRetentionDays(14).
WithJSONFormat(true).
WithTimeFormat("2006-01-02 15:04:05").
WithAddSource(true)
if config.AppName != "builder-test" {
t.Errorf("Expected app name 'builder-test', got '%s'", config.AppName)
}
if config.LogDir != "builder-logs" {
t.Errorf("Expected log dir 'builder-logs', got '%s'", config.LogDir)
}
if config.LogLevel != slog.LevelDebug {
t.Error("Expected log level to be DEBUG")
}
if config.RetentionDays != 14 {
t.Errorf("Expected retention days 14, got %d", config.RetentionDays)
}
if !config.JSONFormat {
t.Error("Expected JSON format to be enabled")
}
if config.TimeFormat != "2006-01-02 15:04:05" {
t.Errorf("Expected custom time format, got '%s'", config.TimeFormat)
}
if !config.AddSource {
t.Error("Expected add-source to be disabled")
}
}
func TestFileRotation(t *testing.T) {
config := DefaultConfig().
WithAppName("test-rotation").
WithLogDir("test-logs-rotation").
WithLogLevel(slog.LevelDebug)
logger, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("test-logs-rotation")
logger.Info("Before rotation")
err = logger.RotateNow()
if err != nil {
t.Errorf("Failed to rotate logs: %v", err)
}
logger.Info("After rotation")
// Check that files exist
files, err := logger.GetLogFiles()
if err != nil {
t.Errorf("Failed to get log files: %v", err)
}
if len(files) == 0 {
t.Error("Expected at least one log file")
}
}
func TestLogFileNaming(t *testing.T) {
config := DefaultConfig().
WithAppName("naming-test").
WithLogDir("test-logs-naming")
logger, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("test-logs-naming")
logger.Info("Test message")
files, err := logger.GetLogFiles()
if err != nil {
t.Errorf("Failed to get log files: %v", err)
}
today := time.Now().Format("2006-01-02")
expectedInfo := "naming-test_" + today + ".log"
expectedError := "naming-test_error_" + today + ".log"
var foundInfo, foundError bool
for _, file := range files {
if file == expectedInfo {
foundInfo = true
}
if file == expectedError {
foundError = true
}
}
if !foundInfo {
t.Errorf("Expected to find info log file '%s', got files: %v", expectedInfo, files)
}
if !foundError {
t.Errorf("Expected to find error log file '%s', got files: %v", expectedError, files)
}
}
func TestCleanup(t *testing.T) {
config := DefaultConfig().
WithAppName("test-cleanup").
WithLogDir("test-logs-cleanup").
WithRetentionDays(1) // Keep only 1 day
logger, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("test-logs-cleanup")
oldDate := time.Now().AddDate(0, 0, -2).Format("2006-01-02")
oldFile := filepath.Join("test-logs-cleanup", "test-cleanup_"+oldDate+".log")
file, err := os.Create(oldFile)
if err != nil {
t.Fatalf("Failed to create old test file: %v", err)
}
file.Close()
twoDaysAgo := time.Now().AddDate(0, 0, -2)
os.Chtimes(oldFile, twoDaysAgo, twoDaysAgo)
logger.CleanupNow()
time.Sleep(100 * time.Millisecond)
if _, err := os.Stat(oldFile); !os.IsNotExist(err) {
t.Error("Expected old log file to be removed")
}
}
func TestIsOurLogFile(t *testing.T) {
config := DefaultConfig().WithAppName("myapp")
logger := &Logger{config: config}
tests := []struct {
filename string
expected bool
}{
{"myapp_2024-01-01.log", true},
{"myapp_error_2024-01-01.log", true},
{"otherapp_2024-01-01.log", false},
{"myapp.txt", false},
{"random.log", false},
{"myapp_", false},
}
for _, test := range tests {
result := logger.isOurLogFile(test.filename)
if result != test.expected {
t.Errorf("isOurLogFile(%s) = %v, expected %v", test.filename, result, test.expected)
}
}
}
func BenchmarkLogging(b *testing.B) {
config := DefaultConfig().
WithAppName("bench").
WithLogDir("bench-logs").
WithLogLevel(slog.LevelDebug)
logger, err := New(config)
if err != nil {
b.Fatalf("Failed to create logger: %v", err)
}
defer logger.Close()
defer os.RemoveAll("bench-logs")
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info("Benchmark message", "iteration", b.N, "timestamp", time.Now())
}
})
}
func TestLogger_BufferedWrites(t *testing.T) {
tempDir := filepath.Join(os.TempDir(), "islogger_buffer_test")
defer os.RemoveAll(tempDir)
config := DefaultConfig().
WithLogDir(tempDir).
WithAppName("buffer_test").
WithLogLevel(slog.LevelDebug). // Enable debug to see INFO messages
WithBufferSize(1024).
WithFlushInterval(100 * time.Millisecond).
WithFlushOnLevel(slog.LevelError)
l, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer l.Close()
// Write some logs
l.Info("This is an info message")
l.Debug("This is a debug message")
l.Warn("This is a warning message")
// Check that files exist but may not have content yet (buffered)
infoFile := filepath.Join(tempDir, "buffer_test_"+time.Now().Format("2006-01-02")+".log")
errorFile := filepath.Join(tempDir, "buffer_test_error_"+time.Now().Format("2006-01-02")+".log")
// Files should exist
if _, err := os.Stat(infoFile); os.IsNotExist(err) {
t.Fatal("Info log file should exist")
}
if _, err := os.Stat(errorFile); os.IsNotExist(err) {
t.Fatal("Error log file should exist")
}
// Manual flush
err = l.Flush()
if err != nil {
t.Fatalf("Failed to flush logger: %v", err)
}
// Now files should have content
infoContent, err := os.ReadFile(infoFile)
if err != nil {
t.Fatalf("Failed to read info file: %v", err)
}
if !strings.Contains(string(infoContent), "This is an info message") {
t.Fatal("Info file should contain info message")
}
errorContent, err := os.ReadFile(errorFile)
if err != nil {
t.Fatalf("Failed to read error file: %v", err)
}
if !strings.Contains(string(errorContent), "This is a warning message") {
t.Fatal("Error file should contain warning message")
}
}
func TestLogger_BufferedWritesWithoutBuffering(t *testing.T) {
tempDir := filepath.Join(os.TempDir(), "islogger_nobuffer_test")
defer os.RemoveAll(tempDir)
config := DefaultConfig().
WithLogDir(tempDir).
WithAppName("nobuffer_test").
WithLogLevel(slog.LevelDebug). // Enable debug to see INFO messages
WithoutBuffering() // Disable buffering
l, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer l.Close()
// Write some logs
l.Info("This is an info message")
l.Warn("This is a warning message")
// Files should have content immediately (no buffering)
infoFile := filepath.Join(tempDir, "nobuffer_test_"+time.Now().Format("2006-01-02")+".log")
errorFile := filepath.Join(tempDir, "nobuffer_test_error_"+time.Now().Format("2006-01-02")+".log")
infoContent, err := os.ReadFile(infoFile)
if err != nil {
t.Fatalf("Failed to read info file: %v", err)
}
if !strings.Contains(string(infoContent), "This is an info message") {
t.Fatal("Info file should immediately contain info message")
}
errorContent, err := os.ReadFile(errorFile)
if err != nil {
t.Fatalf("Failed to read error file: %v", err)
}
if !strings.Contains(string(errorContent), "This is a warning message") {
t.Fatal("Error file should immediately contain warning message")
}
}
func TestLogger_BufferedWritesAutoFlush(t *testing.T) {
tempDir := filepath.Join(os.TempDir(), "islogger_autoflush_test")
defer os.RemoveAll(tempDir)
config := DefaultConfig().
WithLogDir(tempDir).
WithAppName("autoflush_test").
WithLogLevel(slog.LevelDebug). // Enable debug to see INFO messages
WithBufferSize(1024).
WithFlushInterval(50 * time.Millisecond) // Very short interval
l, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer l.Close()
// Write a log
l.Info("This is an auto-flush test message")
infoFile := filepath.Join(tempDir, "autoflush_test_"+time.Now().Format("2006-01-02")+".log")
// Wait for auto-flush
time.Sleep(100 * time.Millisecond)
// File should have content due to auto-flush
infoContent, err := os.ReadFile(infoFile)
if err != nil {
t.Fatalf("Failed to read info file: %v", err)
}
if !strings.Contains(string(infoContent), "This is an auto-flush test message") {
t.Fatal("Info file should contain auto-flushed message")
}
}
func TestLogger_BufferedWritesImmediateFlushOnError(t *testing.T) {
tempDir := filepath.Join(os.TempDir(), "islogger_errorflush_test")
defer os.RemoveAll(tempDir)
config := DefaultConfig().
WithLogDir(tempDir).
WithAppName("errorflush_test").
WithBufferSize(1024).
WithFlushOnLevel(slog.LevelError) // Flush immediately on errors
l, err := New(config)
if err != nil {
t.Fatalf("Failed to create logger: %v", err)
}
defer l.Close()
// Write an error log
l.Error("This is an error message")
errorFile := filepath.Join(tempDir, "errorflush_test_error_"+time.Now().Format("2006-01-02")+".log")
// File should have content immediately due to error level flush
errorContent, err := os.ReadFile(errorFile)
if err != nil {
t.Fatalf("Failed to read error file: %v", err)
}
if !strings.Contains(string(errorContent), "This is an error message") {
t.Fatal("Error file should immediately contain error message")
}
}