163 lines
3.9 KiB
Go
163 lines
3.9 KiB
Go
package logger
|
|
|
|
import (
|
|
"bytes"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLoggerLevels(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(LevelInfo, false)
|
|
logger.SetOutput(&buf)
|
|
|
|
// Debug should be below threshold
|
|
logger.Debug("This should not appear")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
if buf.Len() > 0 {
|
|
t.Error("Debug message appeared when it should be filtered")
|
|
}
|
|
|
|
// Info and above should appear
|
|
logger.Info("Info message")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
if !strings.Contains(buf.String(), "[INF]") {
|
|
t.Errorf("Info message not logged, got: %q", buf.String())
|
|
}
|
|
buf.Reset()
|
|
|
|
logger.Warning("Warning message")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
if !strings.Contains(buf.String(), "[WRN]") {
|
|
t.Errorf("Warning message not logged, got: %q", buf.String())
|
|
}
|
|
buf.Reset()
|
|
|
|
logger.Error("Error message")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
if !strings.Contains(buf.String(), "[ERR]") {
|
|
t.Errorf("Error message not logged, got: %q", buf.String())
|
|
}
|
|
buf.Reset()
|
|
|
|
// Test format strings
|
|
logger.Info("Count: %d", 42)
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
if !strings.Contains(buf.String(), "Count: 42") {
|
|
t.Errorf("Formatted message not logged correctly, got: %q", buf.String())
|
|
}
|
|
buf.Reset()
|
|
|
|
// Test changing level
|
|
logger.SetLevel(LevelError)
|
|
logger.Info("This should not appear")
|
|
logger.Warning("This should not appear")
|
|
if buf.Len() > 0 {
|
|
t.Error("Messages below threshold appeared")
|
|
}
|
|
|
|
logger.Error("Error should appear")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
if !strings.Contains(buf.String(), "[ERR]") {
|
|
t.Errorf("Error message not logged after level change, got: %q", buf.String())
|
|
}
|
|
}
|
|
|
|
func TestLoggerConcurrency(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(LevelDebug, false)
|
|
logger.SetOutput(&buf)
|
|
|
|
// Log a bunch of messages concurrently
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 100; i++ {
|
|
wg.Add(1)
|
|
go func(n int) {
|
|
defer wg.Done()
|
|
logger.Info("Concurrent message %d", n)
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
|
|
// Wait for processing
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
// Check all messages were logged
|
|
content := buf.String()
|
|
for i := 0; i < 100; i++ {
|
|
msg := "Concurrent message " + strconv.Itoa(i)
|
|
if !strings.Contains(content, msg) && !strings.Contains(content, "Concurrent message") {
|
|
t.Errorf("Missing concurrent messages")
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoggerColors(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
logger := New(LevelInfo, true)
|
|
logger.SetOutput(&buf)
|
|
|
|
// Test with color
|
|
logger.Info("Colored message")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
|
|
content := buf.String()
|
|
t.Logf("Colored output: %q", content) // Print actual output for diagnosis
|
|
if !strings.Contains(content, "\033[") {
|
|
t.Errorf("Color codes not present when enabled, got: %q", content)
|
|
}
|
|
|
|
buf.Reset()
|
|
logger.DisableColors()
|
|
logger.Info("Non-colored message")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
|
|
content = buf.String()
|
|
if strings.Contains(content, "\033[") {
|
|
t.Errorf("Color codes present when disabled, got: %q", content)
|
|
}
|
|
}
|
|
|
|
func TestDefaultLogger(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
SetOutput(&buf)
|
|
|
|
Info("Test default logger")
|
|
time.Sleep(10 * time.Millisecond) // Wait for processing
|
|
|
|
content := buf.String()
|
|
if !strings.Contains(content, "[INF]") {
|
|
t.Errorf("Default logger not working, got: %q", content)
|
|
}
|
|
}
|
|
|
|
func BenchmarkLogger(b *testing.B) {
|
|
var buf bytes.Buffer
|
|
logger := New(LevelInfo, false)
|
|
logger.SetOutput(&buf)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
logger.Info("Benchmark message %d", i)
|
|
}
|
|
}
|
|
|
|
func BenchmarkLoggerParallel(b *testing.B) {
|
|
var buf bytes.Buffer
|
|
logger := New(LevelInfo, false)
|
|
logger.SetOutput(&buf)
|
|
|
|
b.ResetTimer()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
logger.Info("Parallel benchmark message %d", i)
|
|
i++
|
|
}
|
|
})
|
|
}
|