- Add configurable log levels: DEBUG, INFO, WARN, ERROR - Replace debug.enabled with log.level in configuration - Add Warn/Warnf methods for warning messages - Log orphan events and buffer overflow as WARN - Log parse errors as WARN - Log raw events and correlations as DEBUG Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
143 lines
3.1 KiB
Go
143 lines
3.1 KiB
Go
package observability
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestNewLogger(t *testing.T) {
|
|
logger := NewLogger("test")
|
|
if logger == nil {
|
|
t.Fatal("expected non-nil logger")
|
|
}
|
|
if logger.prefix != "test" {
|
|
t.Errorf("expected prefix 'test', got %s", logger.prefix)
|
|
}
|
|
}
|
|
|
|
func TestLogger_Info(t *testing.T) {
|
|
logger := NewLoggerWithLevel("test", "INFO")
|
|
|
|
// INFO should be logged
|
|
if !logger.ShouldLog(INFO) {
|
|
t.Error("INFO should be enabled")
|
|
}
|
|
logger.Info("test message")
|
|
}
|
|
|
|
func TestLogger_Error(t *testing.T) {
|
|
logger := NewLoggerWithLevel("test", "ERROR")
|
|
|
|
// ERROR should be logged
|
|
if !logger.ShouldLog(ERROR) {
|
|
t.Error("ERROR should be enabled")
|
|
}
|
|
logger.Error("error message", nil)
|
|
}
|
|
|
|
func TestLogger_Debug(t *testing.T) {
|
|
logger := NewLogger("test")
|
|
|
|
// Debug should be disabled by default (INFO is default)
|
|
if logger.ShouldLog(DEBUG) {
|
|
t.Error("debug should be disabled by default")
|
|
}
|
|
|
|
// Enable debug level
|
|
logger.SetLevel("DEBUG")
|
|
if !logger.ShouldLog(DEBUG) {
|
|
t.Error("debug should be enabled after SetLevel(DEBUG)")
|
|
}
|
|
|
|
// Just verify ShouldLog works correctly
|
|
logger.Debug("test message") // Should not panic
|
|
}
|
|
|
|
func TestLogger_SetLevel(t *testing.T) {
|
|
logger := NewLogger("test")
|
|
|
|
// Default is INFO
|
|
if logger.minLevel != INFO {
|
|
t.Error("default level should be INFO")
|
|
}
|
|
|
|
// Test all levels
|
|
logger.SetLevel("DEBUG")
|
|
if !logger.ShouldLog(DEBUG) {
|
|
t.Error("DEBUG should be enabled after SetLevel(DEBUG)")
|
|
}
|
|
|
|
logger.SetLevel("INFO")
|
|
if logger.ShouldLog(DEBUG) {
|
|
t.Error("DEBUG should be disabled after SetLevel(INFO)")
|
|
}
|
|
if !logger.ShouldLog(INFO) {
|
|
t.Error("INFO should be enabled after SetLevel(INFO)")
|
|
}
|
|
|
|
logger.SetLevel("WARN")
|
|
if logger.ShouldLog(INFO) {
|
|
t.Error("INFO should be disabled after SetLevel(WARN)")
|
|
}
|
|
if !logger.ShouldLog(WARN) {
|
|
t.Error("WARN should be enabled after SetLevel(WARN)")
|
|
}
|
|
|
|
logger.SetLevel("ERROR")
|
|
if logger.ShouldLog(WARN) {
|
|
t.Error("WARN should be disabled after SetLevel(ERROR)")
|
|
}
|
|
if !logger.ShouldLog(ERROR) {
|
|
t.Error("ERROR should be enabled after SetLevel(ERROR)")
|
|
}
|
|
}
|
|
|
|
func TestParseLogLevel(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
expected LogLevel
|
|
}{
|
|
{"DEBUG", DEBUG},
|
|
{"debug", DEBUG},
|
|
{"INFO", INFO},
|
|
{"info", INFO},
|
|
{"WARN", WARN},
|
|
{"warn", WARN},
|
|
{"WARNING", WARN},
|
|
{"ERROR", ERROR},
|
|
{"error", ERROR},
|
|
{"", INFO}, // default
|
|
{"invalid", INFO}, // default
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
result := ParseLogLevel(tt.input)
|
|
if result != tt.expected {
|
|
t.Errorf("ParseLogLevel(%q) = %v, want %v", tt.input, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLogger_WithFields(t *testing.T) {
|
|
logger := NewLogger("test")
|
|
fieldsLogger := logger.WithFields(map[string]any{
|
|
"key1": "value1",
|
|
"key2": 42,
|
|
})
|
|
|
|
if fieldsLogger == logger {
|
|
t.Error("expected different logger instance")
|
|
}
|
|
if len(fieldsLogger.fields) != 2 {
|
|
t.Errorf("expected 2 fields, got %d", len(fieldsLogger.fields))
|
|
}
|
|
}
|
|
|
|
func TestLogger_Name(t *testing.T) {
|
|
logger := NewLogger("myservice")
|
|
if logger.prefix != "myservice" {
|
|
t.Errorf("expected prefix 'myservice', got %s", logger.prefix)
|
|
}
|
|
}
|