Documentation
¶
Overview ¶
Package log provides error code definitions for structured logging.
Package log provides structured JSON logging with trace context support. formatter.go: Pretty format output for development (ADR-029).
Package log provides structured JSON logging with trace context support.
Index ¶
- Constants
- func Debug(msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorStructured(msg string, event string, durationMs float64, sessionID, flowID string, ...)
- func FormatEntry(entry Entry) []byte
- func Info(msg string, args ...any)
- func InfoStructured(msg string, event string, durationMs float64, sessionID, flowID string, ...)
- func InitFromEnv()
- func MaskAPIKey(key string) string
- func MaskEmail(email string) string
- func MaskPassword(_ string) string
- func MaskPath(path string) string
- func MaskPhone(phone string) string
- func SetFormat(format Format)
- func SetLevel(level Level)
- func SetOutput(w io.Writer)
- func Warn(msg string, args ...any)
- func WarnStructured(msg string, event string, durationMs float64, sessionID, flowID string, ...)
- type Entry
- type Format
- type Level
- type Logger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorStructured(msg string, event string, durationMs float64, sessionID, flowID string, ...)
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoAudit(msg string, args ...any)
- func (l *Logger) InfoEvent(msg string, args ...any)
- func (l *Logger) InfoState(msg string, args ...any)
- func (l *Logger) InfoStructured(msg string, event string, durationMs float64, sessionID, flowID string, ...)
- func (l *Logger) LogStructured(level Level, msg string, event string, durationMs float64, ...)
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnPerf(msg string, args ...any)
- func (l *Logger) WarnStructured(msg string, event string, durationMs float64, sessionID, flowID string, ...)
- func (l *Logger) WithErrorCode(ec int) *Logger
- func (l *Logger) WithEvent(ev string) *Logger
- func (l *Logger) WithModule(mod string) *Logger
- type PrettyFormatter
Constants ¶
const ( ECConfigNotFound = 1001 // 配置文件未找到 ECConfigParseError = 1002 // 配置解析失败 ECConfigValidation = 1003 // 配置验证失败 ECConfigEnvMissing = 1004 // 环境变量缺失 )
Config errors (1000-1999)
const ( ECDBOpenFailed = 2001 // 数据库打开失败 ECDBMigrateFailed = 2002 // 数据库迁移失败 ECDBQueryFailed = 2003 // 查询执行失败 ECDBTxFailed = 2004 // 事务失败 )
Storage errors (2000-2999)
const ( ECMemSessionNotFound = 3001 // 会话不存在 ECMemVectorFailed = 3002 // 向量化失败 ECMemRAGSearchFailed = 3003 // RAG 搜索失败 ECMemStorageFull = 3004 // 内存存储已满 )
Memory errors (3000-3999)
const ( ECAgentIntentFailed = 4001 // 意图解析失败 ECAgentPlanFailed = 4002 // 计划生成失败 ECAgentExecuteFailed = 4003 // 执行步骤失败 ECAgentReviewFailed = 4004 // 审查失败 ECAgentLoopDetected = 4005 // 检测到循环 ECAgentTimeout = 4006 // Agent 执行超时 )
Agent errors (4000-4999)
const ( ECLLMRequestFailed = 5001 // LLM 请求失败 ECLLMRateLimited = 5002 // 触发速率限制 ECLLMTokenExceeded = 5003 // Token 超限 ECLLMInvalidResponse = 5004 // 响应格式无效 ECLLMStreamInterrupted = 5005 // 流式响应中断 ECLLMAPIKeyInvalid = 5006 // API Key 无效 )
LLM errors (5000-5999)
const ( ECBrowserLaunchFailed = 6001 // 浏览器启动失败 ECBrowserElementNotFound = 6003 // 元素未找到 ECBrowserClickFailed = 6004 // 点击失败 ECBrowserInputFailed = 6005 // 输入失败 ECBrowserTimeout = 6006 // 操作超时 ECBrowserCDPError = 6007 // CDP 协议错误 ECBrowserOccluded = 6008 // 元素被遮挡 )
Browser errors (6000-6999)
const ( ECSkillNotFound = 7001 // Skill 未找到 ECSkillExecuteFailed = 7002 // Skill 执行失败 ECSkillInvalidArgs = 7003 // 参数无效 )
Skill errors (7000-7999)
const ( ECWebUIBindFailed = 8001 // 端口绑定失败 ECWebUIHandlerError = 8002 // 处理器错误 )
WebUI errors (8000-8999)
const ( ECDesktopWindowFailed = 9001 // 窗口创建失败 ECDesktopBindingError = 9002 // 绑定错误 ECDesktopFrontendError = 9003 // 前端错误 )
Desktop errors (9000-9999)
Variables ¶
This section is empty.
Functions ¶
func ErrorStructured ¶
func ErrorStructured(msg string, event string, durationMs float64, sessionID, flowID string, extra map[string]interface{})
ErrorStructured logs an ERROR level structured event.
func FormatEntry ¶
FormatEntry formats an entry based on current format setting.
func InfoStructured ¶
func InfoStructured(msg string, event string, durationMs float64, sessionID, flowID string, extra map[string]interface{})
InfoStructured logs an INFO level structured event.
func InitFromEnv ¶
func InitFromEnv()
InitFromEnv initializes log format from LOG_FORMAT environment variable (ADR-029).
func MaskAPIKey ¶
MaskAPIKey masks API key for logging (shows first 6 chars).
Types ¶
type Entry ¶
type Entry struct {
L string `json:"l"` // 级别
T string `json:"t"` // 时间戳 YYYYMMDDHHmmss.SSS
P int `json:"p"` // 进程ID
G int64 `json:"g"` // 协程ID
TID string `json:"tid,omitempty"` // 业务流ID
Span string `json:"span,omitempty"` // 当前 Span ID
PSpan string `json:"pspan,omitempty"` // 父 Span ID
STG string `json:"stg,omitempty"` // 阶段 (业务流优先)
Mod string `json:"mod,omitempty"` // 模块
EV string `json:"ev,omitempty"` // 事件上下文
F string `json:"f,omitempty"` // 源码位置
EC int `json:"ec,omitempty"` // 错误码
Msg string `json:"msg"` // 消息
Dur float64 `json:"dur,omitempty"` // 耗时(毫秒)
Event string `json:"event,omitempty"` // 事件类型 (stage_enter/exit, llm_call, memory_enrich)
DurationMs float64 `json:"duration_ms,omitempty"` // 事件耗时(毫秒)
SessionID string `json:"session_id,omitempty"` // 会话ID
FlowID string `json:"flow_id,omitempty"` // 流程ID
Extra map[string]interface{} `json:"extra,omitempty"` // 额外字段 (event-specific)
}
Entry represents a log entry in JSON format. Schema: l/t/p/g/tid/span/pspan/stg/mod/ev/f/ec/msg/dur/event/duration_ms/session_id/flow_id/extra
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger provides structured logging.
func (*Logger) ErrorStructured ¶
func (l *Logger) ErrorStructured(msg string, event string, durationMs float64, sessionID, flowID string, extra map[string]interface{})
ErrorStructured logs an ERROR level structured event.
func (*Logger) InfoStructured ¶
func (l *Logger) InfoStructured(msg string, event string, durationMs float64, sessionID, flowID string, extra map[string]interface{})
InfoStructured logs an INFO level structured event.
func (*Logger) LogStructured ¶
func (l *Logger) LogStructured(level Level, msg string, event string, durationMs float64, sessionID, flowID string, extra map[string]interface{})
LogStructured logs a structured event with flat fields for jq queryability. All numeric fields are preserved as JSON numbers (not strings).
func (*Logger) WarnStructured ¶
func (l *Logger) WarnStructured(msg string, event string, durationMs float64, sessionID, flowID string, extra map[string]interface{})
WarnStructured logs a WARN level structured event.
func (*Logger) WithErrorCode ¶
WithErrorCode returns a new logger with error code.
func (*Logger) WithModule ¶
WithModule returns a new logger with specified module name.
type PrettyFormatter ¶
type PrettyFormatter struct {
// contains filtered or unexported fields
}
PrettyFormatter formats log entries for human readability (ADR-029).
func NewPrettyFormatter ¶
func NewPrettyFormatter(output io.Writer) *PrettyFormatter
NewPrettyFormatter creates a new PrettyFormatter.
func (*PrettyFormatter) Format ¶
func (f *PrettyFormatter) Format(entry Entry) string
Format formats a log entry in pretty format.
func (*PrettyFormatter) SetColorEnabled ¶
func (f *PrettyFormatter) SetColorEnabled(enabled bool)
SetColorEnabled enables or disables color output.
func (*PrettyFormatter) SetSlowThreshold ¶
func (f *PrettyFormatter) SetSlowThreshold(d time.Duration)
SetSlowThreshold sets the threshold for slow operation highlighting.
func (*PrettyFormatter) Write ¶
func (f *PrettyFormatter) Write(entry Entry)
Write writes a formatted entry to the output.