日志系统
OpenClaw 使用结构化日志框架,支持子系统分层、敏感数据脱敏和诊断事件记录。
源码位置
src/logging/
├── logger.ts # 核心日志实现
├── subsystem.ts # 子系统日志器
├── console.ts # 控制台输出格式化
├── diagnostic.ts # 诊断事件日志
├── redact.ts # 敏感数据脱敏
└── *.test.ts # 测试
src/logger.ts # 顶层日志配置日志架构
子系统日志
Source: src/logging/subsystem.ts
日志通过子系统分层,每个模块有自己的日志器:
typescript
// Subsystem logging (simplified)
// Each module creates a subsystem logger
// Supports hierarchical naming (e.g., "gateway.auth", "agent.tools")
// Verbosity controlled per subsystem子系统示例:
| 子系统 | 说明 |
|---|---|
gateway | Gateway 核心 |
gateway.auth | 认证模块 |
gateway.methods | 方法派发 |
agent | Agent 运行时 |
agent.tools | 工具执行 |
channels.telegram | Telegram 通道 |
channels.discord | Discord 通道 |
plugins | 插件系统 |
Console Capture
Source: src/logging.ts
enableConsoleCapture() 拦截所有 console.* 调用,将它们转换为结构化日志:
typescript
// src/logging.ts (simplified)
function enableConsoleCapture(): void {
// Intercept console.log, console.error, etc.
// Convert to structured log entries
// Preserve original stdout/stderr behavior
}这确保即使第三方库使用 console.log(),输出也会被结构化记录。
敏感数据脱敏
Source: src/logging/redact.ts
日志中的敏感数据会被自动脱敏:
typescript
// Redaction rules (simplified)
// - API tokens → "***"
// - Phone numbers → "***1234"
// - Email addresses → "u***@example.com"
// - Passwords → "[REDACTED]"脱敏在日志输出前自动应用,确保敏感信息不会泄露到日志文件中。
诊断日志
Source: src/logging/diagnostic.ts
诊断事件用于追踪系统行为和性能:
typescript
// Diagnostic events
// - Operation timing
// - Error traces with context
// - Performance metrics
// - System state snapshotsVerbose 级别
日志详细程度通过 verbose level 控制:
| 级别 | 输出内容 |
|---|---|
| 0 | 仅错误和警告 |
| 1 | 基本操作信息 |
| 2 | 详细操作信息 |
| 3 | 调试级别输出 |
WebSocket 日志
Source: src/gateway/ws-log.ts
Gateway 的 WebSocket 通信有专门的日志模块:
typescript
// WebSocket logging (simplified)
// - Connection events (connect, disconnect, error)
// - Method calls (request, response)
// - Event emissions
// - Payload sizes and timing小结
- 结构化日志框架,支持子系统分层和层级命名
- Console Capture 拦截所有
console.*调用 - 自动脱敏 防止敏感数据(token、密码、手机号)泄露
- 诊断事件 追踪系统行为和性能
- Verbose 级别 (0-3) 控制日志详细程度
- WebSocket 通信有专门的日志记录