log

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 10 Imported by: 0

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

View Source
const (
	ECConfigNotFound   = 1001 // 配置文件未找到
	ECConfigParseError = 1002 // 配置解析失败
	ECConfigValidation = 1003 // 配置验证失败
	ECConfigEnvMissing = 1004 // 环境变量缺失
)

Config errors (1000-1999)

View Source
const (
	ECDBOpenFailed    = 2001 // 数据库打开失败
	ECDBMigrateFailed = 2002 // 数据库迁移失败
	ECDBQueryFailed   = 2003 // 查询执行失败
	ECDBTxFailed      = 2004 // 事务失败
)

Storage errors (2000-2999)

View Source
const (
	ECMemSessionNotFound = 3001 // 会话不存在
	ECMemVectorFailed    = 3002 // 向量化失败
	ECMemRAGSearchFailed = 3003 // RAG 搜索失败
	ECMemStorageFull     = 3004 // 内存存储已满
)

Memory errors (3000-3999)

View Source
const (
	ECAgentIntentFailed  = 4001 // 意图解析失败
	ECAgentPlanFailed    = 4002 // 计划生成失败
	ECAgentExecuteFailed = 4003 // 执行步骤失败
	ECAgentReviewFailed  = 4004 // 审查失败
	ECAgentLoopDetected  = 4005 // 检测到循环
	ECAgentTimeout       = 4006 // Agent 执行超时
)

Agent errors (4000-4999)

View Source
const (
	ECLLMRequestFailed     = 5001 // LLM 请求失败
	ECLLMRateLimited       = 5002 // 触发速率限制
	ECLLMTokenExceeded     = 5003 // Token 超限
	ECLLMInvalidResponse   = 5004 // 响应格式无效
	ECLLMStreamInterrupted = 5005 // 流式响应中断
	ECLLMAPIKeyInvalid     = 5006 // API Key 无效
)

LLM errors (5000-5999)

View Source
const (
	ECBrowserLaunchFailed    = 6001 // 浏览器启动失败
	ECBrowserNavigateFailed  = 6002 // 导航失败
	ECBrowserElementNotFound = 6003 // 元素未找到
	ECBrowserClickFailed     = 6004 // 点击失败
	ECBrowserInputFailed     = 6005 // 输入失败
	ECBrowserTimeout         = 6006 // 操作超时
	ECBrowserCDPError        = 6007 // CDP 协议错误
	ECBrowserOccluded        = 6008 // 元素被遮挡
)

Browser errors (6000-6999)

View Source
const (
	ECSkillNotFound      = 7001 // Skill 未找到
	ECSkillExecuteFailed = 7002 // Skill 执行失败
	ECSkillInvalidArgs   = 7003 // 参数无效
)

Skill errors (7000-7999)

View Source
const (
	ECWebUIBindFailed   = 8001 // 端口绑定失败
	ECWebUIHandlerError = 8002 // 处理器错误
)

WebUI errors (8000-8999)

View Source
const (
	ECDesktopWindowFailed  = 9001 // 窗口创建失败
	ECDesktopBindingError  = 9002 // 绑定错误
	ECDesktopFrontendError = 9003 // 前端错误
)

Desktop errors (9000-9999)

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...any)

Debug logs at DEBUG level.

func Error

func Error(msg string, args ...any)

Error logs at ERROR level.

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

func FormatEntry(entry Entry) []byte

FormatEntry formats an entry based on current format setting.

func Info

func Info(msg string, args ...any)

Info logs at INFO level.

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

func MaskAPIKey(key string) string

MaskAPIKey masks API key for logging (shows first 6 chars).

func MaskEmail

func MaskEmail(email string) string

MaskEmail masks email for logging (u***@example.com).

func MaskPassword

func MaskPassword(_ string) string

MaskPassword masks password for logging.

func MaskPath

func MaskPath(path string) string

MaskPath masks username in file path (/home/***/).

func MaskPhone

func MaskPhone(phone string) string

MaskPhone masks phone number for logging.

func SetFormat

func SetFormat(format Format)

SetFormat sets the global log format. Called once at startup based on LOG_FORMAT environment variable.

func SetLevel

func SetLevel(level Level)

SetLevel sets the global log level.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the global log output.

func Warn

func Warn(msg string, args ...any)

Warn logs at WARN level.

func WarnStructured

func WarnStructured(msg string, event string, durationMs float64, sessionID, flowID string, extra map[string]interface{})

WarnStructured logs a WARN level structured event.

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 Format

type Format string

Format represents log output format.

const (
	// FormatJSON outputs structured JSON (default, production).
	FormatJSON Format = "json"
	// FormatPretty outputs human-readable colored text (development).
	FormatPretty Format = "pretty"
)

func GetFormat

func GetFormat() Format

GetFormat returns the current log format.

type Level

type Level int

Level represents log level.

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
)

func (Level) String

func (l Level) String() string

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger provides structured logging.

func Module

func Module(name string) *Logger

Module returns a logger with specified module name.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs at DEBUG level.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error logs at ERROR level.

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) Info

func (l *Logger) Info(msg string, args ...any)

Info logs at INFO level.

func (*Logger) InfoAudit

func (l *Logger) InfoAudit(msg string, args ...any)

InfoAudit logs an audit event with category=audit.

func (*Logger) InfoEvent

func (l *Logger) InfoEvent(msg string, args ...any)

InfoEvent logs an event with category=event.

func (*Logger) InfoState

func (l *Logger) InfoState(msg string, args ...any)

InfoState logs a state change with category=state.

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) Warn

func (l *Logger) Warn(msg string, args ...any)

Warn logs at WARN level.

func (*Logger) WarnPerf

func (l *Logger) WarnPerf(msg string, args ...any)

WarnPerf logs a performance warning with category=perf.

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

func (l *Logger) WithErrorCode(ec int) *Logger

WithErrorCode returns a new logger with error code.

func (*Logger) WithEvent

func (l *Logger) WithEvent(ev string) *Logger

WithEvent returns a new logger with event context (one-time).

func (*Logger) WithModule

func (l *Logger) WithModule(mod string) *Logger

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL