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 != "" {
|
||||
|
||||
Reference in New Issue
Block a user