chore: version 1.0.7 - add log levels
- 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>
This commit is contained in:
@ -1,10 +1,6 @@
|
||||
package observability
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -19,72 +15,107 @@ func TestNewLogger(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLogger_Info(t *testing.T) {
|
||||
// Capture stderr
|
||||
oldStderr := os.Stderr
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stderr = w
|
||||
|
||||
logger := NewLogger("test")
|
||||
logger := NewLoggerWithLevel("test", "INFO")
|
||||
|
||||
// INFO should be logged
|
||||
if !logger.ShouldLog(INFO) {
|
||||
t.Error("INFO should be enabled")
|
||||
}
|
||||
logger.Info("test message")
|
||||
|
||||
w.Close()
|
||||
os.Stderr = oldStderr
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
|
||||
if !strings.Contains(output, "INFO") {
|
||||
t.Error("expected INFO in output")
|
||||
}
|
||||
if !strings.Contains(output, "test message") {
|
||||
t.Error("expected 'test message' in output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogger_Error(t *testing.T) {
|
||||
oldStderr := os.Stderr
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stderr = w
|
||||
|
||||
logger := NewLogger("test")
|
||||
logger := NewLoggerWithLevel("test", "ERROR")
|
||||
|
||||
// ERROR should be logged
|
||||
if !logger.ShouldLog(ERROR) {
|
||||
t.Error("ERROR should be enabled")
|
||||
}
|
||||
logger.Error("error message", nil)
|
||||
|
||||
w.Close()
|
||||
os.Stderr = oldStderr
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
|
||||
if !strings.Contains(output, "ERROR") {
|
||||
t.Error("expected ERROR in output")
|
||||
}
|
||||
if !strings.Contains(output, "error message") {
|
||||
t.Error("expected 'error message' in output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogger_Debug(t *testing.T) {
|
||||
oldStderr := os.Stderr
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stderr = w
|
||||
|
||||
logger := NewLogger("test")
|
||||
logger.Debug("debug message")
|
||||
|
||||
w.Close()
|
||||
os.Stderr = oldStderr
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, r)
|
||||
output := buf.String()
|
||||
|
||||
if !strings.Contains(output, "DEBUG") {
|
||||
t.Error("expected DEBUG in output")
|
||||
|
||||
// Debug should be disabled by default (INFO is default)
|
||||
if logger.ShouldLog(DEBUG) {
|
||||
t.Error("debug should be disabled by default")
|
||||
}
|
||||
if !strings.Contains(output, "debug message") {
|
||||
t.Error("expected 'debug message' in output")
|
||||
|
||||
// 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user