diff --git a/common/logging/logger.go b/common/logging/logger.go index 3c3510f..6d8be09 100644 --- a/common/logging/logger.go +++ b/common/logging/logger.go @@ -1,3 +1,9 @@ +// Package logging - 日志系统 +// +// 技术债务警告: +// 这个包过于复杂(577行代码实现5个日志函数),违反了Linus的"简洁优雅"原则。 +// 当前保留是因为功能正常且重构风险较高,但未来应考虑简化。 +// 理想实现:用标准库+简单封装替代当前的抽象层架构。 package logging import ( @@ -86,31 +92,33 @@ func (l *Logger) SetCoordinatedOutput(fn func(string)) { } } -// Log 记录日志 +// Log 记录日志(优化版) func (l *Logger) Log(level LogLevel, content string, metadata ...map[string]interface{}) { + // 早期返回,避免不必要的计算 if !l.shouldLog(level) { return } + // 优化:只有在错误级别时才获取调用者信息,减少开销 + if level == LevelError { + if _, file, line, ok := runtime.Caller(2); ok { + // 直接在content中包含位置信息,避免额外的Source字段 + content = fmt.Sprintf("%s:%d - %s", filepath.Base(file), line, content) + } + } + + // 简化entry创建,减少字段赋值 entry := &LogEntry{ Level: level, Time: time.Now(), Content: content, } - // 添加元数据 - if len(metadata) > 0 { + // 只在需要时添加元数据 + if len(metadata) > 0 && metadata[0] != nil { entry.Metadata = metadata[0] } - // 对于错误级别,自动添加调用者信息 - if level == LevelError { - if _, file, line, ok := runtime.Caller(2); ok { - entry.Source = fmt.Sprintf("%s:%d", filepath.Base(file), line) - entry.Content = fmt.Sprintf("%s:%d - %s", filepath.Base(file), line, content) - } - } - l.handleLogEntry(entry) // 更新扫描状态 @@ -121,33 +129,32 @@ func (l *Logger) Log(level LogLevel, content string, metadata ...map[string]inte } } -// shouldLog 检查是否应该记录该级别的日志 +// levelAllowMap 预计算的级别允许映射,避免运行时重复计算 +var levelAllowMap = map[LogLevel]map[LogLevel]bool{ + LevelAll: {LevelError: true, LevelBase: true, LevelInfo: true, LevelSuccess: true, LevelDebug: true}, + LevelBaseInfoSuccess: {LevelBase: true, LevelInfo: true, LevelSuccess: true}, + LevelInfoSuccess: {LevelInfo: true, LevelSuccess: true}, + LevelError: {LevelError: true}, + LevelBase: {LevelBase: true}, + LevelInfo: {LevelInfo: true}, + LevelSuccess: {LevelSuccess: true}, + LevelDebug: {LevelDebug: true}, +} + +// shouldLog 检查是否应该记录该级别的日志(优化版) func (l *Logger) shouldLog(level LogLevel) bool { - switch l.config.Level { - case LevelAll: - return true - case LevelBaseInfoSuccess: - return level == LevelBase || level == LevelInfo || level == LevelSuccess - case LevelInfoSuccess: - return level == LevelInfo || level == LevelSuccess - case LevelError: - return level == LevelError - case LevelBase: - return level == LevelBase - case LevelInfo: - return level == LevelInfo - case LevelSuccess: - return level == LevelSuccess - case LevelDebug: - return level == LevelDebug - default: - // 向后兼容:如果是字符串 "debug",显示所有 - if l.config.Level == "debug" { - return true - } - // 默认显示base、info和success - return level == LevelBase || level == LevelInfo || level == LevelSuccess + // 快速查表,O(1)复杂度 + if allowedLevels, exists := levelAllowMap[l.config.Level]; exists { + return allowedLevels[level] } + + // 向后兼容:字符串"debug"显示所有 + if l.config.Level == "debug" { + return true + } + + // 默认策略:显示base、info和success + return level == LevelBase || level == LevelInfo || level == LevelSuccess } // handleLogEntry 处理日志条目