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,34 +1,109 @@
|
||||
package observability
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Logger provides structured logging.
|
||||
type Logger struct {
|
||||
mu sync.Mutex
|
||||
logger *log.Logger
|
||||
prefix string
|
||||
fields map[string]any
|
||||
// LogLevel represents the severity of a log message.
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
DEBUG LogLevel = iota
|
||||
INFO
|
||||
WARN
|
||||
ERROR
|
||||
)
|
||||
|
||||
// ParseLogLevel converts a string to LogLevel.
|
||||
func ParseLogLevel(level string) LogLevel {
|
||||
switch strings.ToUpper(level) {
|
||||
case "DEBUG":
|
||||
return DEBUG
|
||||
case "INFO":
|
||||
return INFO
|
||||
case "WARN", "WARNING":
|
||||
return WARN
|
||||
case "ERROR":
|
||||
return ERROR
|
||||
default:
|
||||
return INFO
|
||||
}
|
||||
}
|
||||
|
||||
// NewLogger creates a new logger.
|
||||
// String returns the string representation of LogLevel.
|
||||
func (l LogLevel) String() string {
|
||||
switch l {
|
||||
case DEBUG:
|
||||
return "DEBUG"
|
||||
case INFO:
|
||||
return "INFO"
|
||||
case WARN:
|
||||
return "WARN"
|
||||
case ERROR:
|
||||
return "ERROR"
|
||||
default:
|
||||
return "INFO"
|
||||
}
|
||||
}
|
||||
|
||||
// Logger provides structured logging.
|
||||
type Logger struct {
|
||||
mu sync.Mutex
|
||||
logger *log.Logger
|
||||
prefix string
|
||||
fields map[string]any
|
||||
minLevel LogLevel
|
||||
}
|
||||
|
||||
// NewLogger creates a new logger with INFO level by default.
|
||||
func NewLogger(prefix string) *Logger {
|
||||
return &Logger{
|
||||
logger: log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds),
|
||||
prefix: prefix,
|
||||
fields: make(map[string]any),
|
||||
logger: log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds),
|
||||
prefix: prefix,
|
||||
fields: make(map[string]any),
|
||||
minLevel: INFO,
|
||||
}
|
||||
}
|
||||
|
||||
// NewLoggerWithLevel creates a new logger with specified minimum level.
|
||||
func NewLoggerWithLevel(prefix string, level string) *Logger {
|
||||
return &Logger{
|
||||
logger: log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds),
|
||||
prefix: prefix,
|
||||
fields: make(map[string]any),
|
||||
minLevel: ParseLogLevel(level),
|
||||
}
|
||||
}
|
||||
|
||||
// SetLevel sets the minimum log level.
|
||||
func (l *Logger) SetLevel(level string) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.minLevel = ParseLogLevel(level)
|
||||
}
|
||||
|
||||
// ShouldLog returns true if the given level should be logged.
|
||||
func (l *Logger) ShouldLog(level LogLevel) bool {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
return level >= l.minLevel
|
||||
}
|
||||
|
||||
// WithFields returns a new logger with additional fields.
|
||||
func (l *Logger) WithFields(fields map[string]any) *Logger {
|
||||
l.mu.Lock()
|
||||
minLevel := l.minLevel
|
||||
l.mu.Unlock()
|
||||
|
||||
newLogger := &Logger{
|
||||
logger: l.logger,
|
||||
prefix: l.prefix,
|
||||
fields: make(map[string]any),
|
||||
logger: l.logger,
|
||||
prefix: l.prefix,
|
||||
fields: make(map[string]any),
|
||||
minLevel: minLevel,
|
||||
}
|
||||
for k, v := range l.fields {
|
||||
newLogger.fields[k] = v
|
||||
@ -41,13 +116,29 @@ func (l *Logger) WithFields(fields map[string]any) *Logger {
|
||||
|
||||
// Info logs an info message.
|
||||
func (l *Logger) Info(msg string) {
|
||||
if !l.ShouldLog(INFO) {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.log("INFO", msg)
|
||||
}
|
||||
|
||||
// Warn logs a warning message.
|
||||
func (l *Logger) Warn(msg string) {
|
||||
if !l.ShouldLog(WARN) {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.log("WARN", msg)
|
||||
}
|
||||
|
||||
// Error logs an error message.
|
||||
func (l *Logger) Error(msg string, err error) {
|
||||
if !l.ShouldLog(ERROR) {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if err != nil {
|
||||
@ -57,13 +148,46 @@ func (l *Logger) Error(msg string, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Debug logs a debug message.
|
||||
// Debug logs a debug message (only if debug level is enabled).
|
||||
func (l *Logger) Debug(msg string) {
|
||||
if !l.ShouldLog(DEBUG) {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.log("DEBUG", msg)
|
||||
}
|
||||
|
||||
// Debugf logs a formatted debug message (only if debug level is enabled).
|
||||
func (l *Logger) Debugf(msg string, args ...any) {
|
||||
if !l.ShouldLog(DEBUG) {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.log("DEBUG", fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
// Warnf logs a formatted warning message.
|
||||
func (l *Logger) Warnf(msg string, args ...any) {
|
||||
if !l.ShouldLog(WARN) {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.log("WARN", fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
// Infof logs a formatted info message.
|
||||
func (l *Logger) Infof(msg string, args ...any) {
|
||||
if !l.ShouldLog(INFO) {
|
||||
return
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.log("INFO", fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
func (l *Logger) log(level, msg string) {
|
||||
prefix := l.prefix
|
||||
if prefix != "" {
|
||||
|
||||
@ -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