- 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>
210 lines
3.9 KiB
Go
210 lines
3.9 KiB
Go
package observability
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
}
|
|
|
|
// 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),
|
|
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),
|
|
minLevel: minLevel,
|
|
}
|
|
for k, v := range l.fields {
|
|
newLogger.fields[k] = v
|
|
}
|
|
for k, v := range fields {
|
|
newLogger.fields[k] = v
|
|
}
|
|
return newLogger
|
|
}
|
|
|
|
// 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 {
|
|
l.log("ERROR", msg+" "+err.Error())
|
|
} else {
|
|
l.log("ERROR", msg)
|
|
}
|
|
}
|
|
|
|
// 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 != "" {
|
|
prefix = "[" + prefix + "] "
|
|
}
|
|
|
|
l.logger.SetPrefix(prefix + level + " ")
|
|
|
|
var args []any
|
|
for k, v := range l.fields {
|
|
args = append(args, k, v)
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
l.logger.Printf(msg+" %+v", args...)
|
|
} else {
|
|
l.logger.Print(msg)
|
|
}
|
|
}
|