Appearance
OpenClaw Skill 调试技巧:常见问题与解决方案
Skill 开发最痛苦的不是写代码,而是调不通:报错信息看不懂、日志找不到、问题复现不了...掌握系统化的调试方法,把 3 小时的排查压缩到 10 分钟。
调试前的准备
开启调试模式
json
// config.json
{
"debug": true,
"logLevel": "debug",
"logFile": "./logs/openclaw.log"
}调试模式 vs 正常模式
| 模式 | 日志级别 | 输出内容 | 适用场景 |
|---|---|---|---|
| 正常模式 | info | 基本信息 | 生产环境 |
| 调试模式 | debug | 详细日志 | 开发调试 |
| 追踪模式 | trace | 全量日志 | 深度排查 |
配置调试工具
bash
# 启用调试日志
openclaw config set logLevel debug
# 查看当前配置
openclaw config list
# 重启服务生效
openclaw restart日志分析
日志位置
OpenClaw/
├── logs/
│ ├── openclaw.log # 主日志
│ ├── skill-{name}.log # Skill 专用日志
│ ├── error.log # 错误日志
│ └── debug.log # 调试日志日志级别说明
| 级别 | 含义 | 示例 |
|---|---|---|
| ERROR | 严重错误 | Skill 加载失败 |
| WARN | 警告信息 | 配置缺失,使用默认值 |
| INFO | 一般信息 | Skill 启动成功 |
| DEBUG | 调试信息 | 函数调用参数 |
| TRACE | 追踪信息 | 完整执行流程 |
实时查看日志
bash
# 实时查看主日志
tail -f logs/openclaw.log
# 只看错误日志
tail -f logs/error.log | grep ERROR
# 查看 Skill 专用日志
tail -f logs/skill-my-skill.log日志关键词搜索
bash
# 搜索特定 Skill 的日志
grep "my-skill" logs/openclaw.log
# 搜索错误信息
grep -i "error\|fail\|exception" logs/openclaw.log
# 搜索最近 100 行的警告
tail -100 logs/openclaw.log | grep WARN常见错误类型
1. Skill 加载失败
错误现象:
[ERROR] Failed to load skill: my-skill
[ERROR] Error: Cannot find module './skills/my-skill'原因分析:
| 原因 | 检查方法 | 解决方案 |
|---|---|---|
| 目录不存在 | ls skills/ | 创建目录 |
| SKILL.md 缺失 | ls skills/my-skill/ | 创建 SKILL.md |
| 文件名错误 | 检查大小写 | 统一命名规范 |
| 权限不足 | ls -la | 添加执行权限 |
解决方案:
bash
# 检查 Skill 目录结构
ls -la skills/my-skill/
# 确保必要文件存在
touch skills/my-skill/SKILL.md
# 添加执行权限
chmod +x skills/my-skill/scripts/*.sh2. 配置错误
错误现象:
[ERROR] Invalid configuration for skill: my-skill
[ERROR] Missing required field: apiKey原因分析:
| 错误类型 | 日志信息 | 解决方案 |
|---|---|---|
| 缺少必填字段 | Missing required field | 补充配置 |
| 类型错误 | Expected string, got number | 修正数据类型 |
| 格式错误 | Invalid URL format | 检查格式 |
| 环境变量缺失 | env.API_KEY not found | 设置环境变量 |
解决方案:
json
// skills/my-skill/config.json
{
"required": ["apiKey"],
"properties": {
"apiKey": {
"type": "string",
"description": "API密钥"
}
}
}3. 执行超时
错误现象:
[ERROR] Skill execution timeout: my-skill
[ERROR] Execution time exceeded 30000ms原因分析:
| 原因 | 检查方法 | 解决方案 |
|---|---|---|
| 网络请求慢 | 检查 API 响应时间 | 增加超时时间 |
| 循环死循环 | 检查代码逻辑 | 修复循环条件 |
| 资源占用高 | 查看系统资源 | 优化算法 |
| 默认超时过短 | 检查超时配置 | 调整 timeout 值 |
解决方案:
json
// SKILL.md 中配置超时
{
"timeout": 60000, // 60秒
"retryCount": 3
}4. 权限错误
错误现象:
[ERROR] Permission denied: skill 'my-skill' cannot access 'browser'
[ERROR] Required permission: browser-use原因分析:
| 错误类型 | 日志信息 | 解决方案 |
|---|---|---|
| 权限未声明 | Permission not declared | 添加 permissions |
| 权限未授予 | Permission not granted | 用户授权 |
| 权限不足 | Insufficient permission | 申请更高权限 |
解决方案:
json
// SKILL.md 中声明权限
{
"permissions": [
"browser-use",
"file-read",
"network-access"
]
}5. 依赖缺失
错误现象:
[ERROR] Module not found: axios
[ERROR] Skill 'my-skill' failed to initialize原因分析:
| 原因 | 检查方法 | 解决方案 |
|---|---|---|
| 未安装依赖 | npm list axios | 安装依赖 |
| 版本不兼容 | 检查 package.json | 指定版本 |
| 路径错误 | 检查 import 路径 | 修正路径 |
解决方案:
bash
# 安装依赖
cd skills/my-skill
npm install axios
# 或在 package.json 中声明
{
"dependencies": {
"axios": "^1.6.0"
}
}调试工具与命令
诊断命令
bash
# 检查 Skill 状态
openclaw skill status my-skill
# 查看配置信息
openclaw skill config my-skill
# 测试 Skill 执行
openclaw skill test my-skill
# 查看依赖关系
openclaw skill deps my-skill调试模式启动
bash
# 单独调试某个 Skill
openclaw debug --skill my-skill
# 带断点启动
openclaw debug --skill my-skill --breakpoint
# 详细输出模式
openclaw debug --skill my-skill --verbose在线诊断
用户:诊断 my-skill 为什么不生效
Agent:[执行诊断命令]
## 诊断结果
### 1. 检查 Skill 加载状态
✅ Skill 已加载
- 路径:skills/my-skill
- 版本:1.0.0
- 状态:active
### 2. 检查配置
❌ 配置缺失
- 缺少字段:apiKey
- 配置文件:config.json
### 3. 检查权限
✅ 权限已声明
- browser-use:已授权
- file-read:已授权
### 4. 检查依赖
✅ 依赖完整
- axios:已安装
- lodash:已安装
### 建议
请在 config.json 中添加 apiKey 配置项。性能调试
性能分析工具
bash
# 分析 Skill 执行时间
openclaw profile --skill my-skill
# 查看内存使用
openclaw memory --skill my-skill
# 分析调用链
openclaw trace --skill my-skill性能报告示例
用户:分析 my-skill 的性能问题
Agent:[执行性能分析]
## 性能分析报告
### 执行时间分析
| 步骤 | 耗时 | 占比 |
|------|------|------|
| 初始化 | 50ms | 5% |
| 数据获取 | 800ms | 80% |
| 数据处理 | 100ms | 10% |
| 结果输出 | 50ms | 5% |
| **总计** | **1000ms** | **100%** |
### 瓶颈识别
🔴 数据获取耗时过长(800ms)
- 原因:API 响应慢
- 建议:添加缓存或并发请求
### 内存使用
| 阶段 | 内存占用 |
|------|----------|
| 初始 | 50MB |
| 峰值 | 150MB |
| 结束 | 80MB |
### 优化建议
1. 添加数据缓存
2. 使用并发请求
3. 减少内存占用常见性能问题
| 问题 | 现象 | 解决方案 |
|---|---|---|
| 内存泄漏 | 内存持续增长 | 检查事件监听器,及时释放 |
| CPU 占用高 | 执行卡顿 | 优化算法,减少循环 |
| 网络超时 | 请求失败 | 添加重试机制 |
| 文件读写慢 | IO 等待 | 使用流式处理 |
调试实战案例
案例一:Skill 不生效
问题描述:
用户:我安装了 my-skill,但调用时没反应调试过程:
## 步骤 1:检查 Skill 状态
$ openclaw skill status my-skill
结果:Skill 未加载
## 步骤 2:查看加载日志
$ grep "my-skill" logs/openclaw.log
[ERROR] Failed to load skill: my-skill
[ERROR] SKILL.md not found
## 步骤 3:检查文件
$ ls skills/my-skill/
result: README.md, index.js
## 步骤 4:创建 SKILL.md
$ touch skills/my-skill/SKILL.md
## 步骤 5:重启服务
$ openclaw restart
## 步骤 6:验证
$ openclaw skill status my-skill
结果:✅ Skill 已加载
## 问题解决
原因:缺少 SKILL.md 文件
解决:创建 SKILL.md 并重启服务案例二:配置错误
问题描述:
用户:my-skill 报错 "Invalid configuration"调试过程:
## 步骤 1:查看错误日志
$ grep "my-skill" logs/error.log
[ERROR] Invalid configuration for skill: my-skill
[ERROR] Missing required field: apiKey
## 步骤 2:检查当前配置
$ openclaw skill config my-skill
{
"name": "my-skill",
"config": {} // 空配置
}
## 步骤 3:查看配置规范
$ cat skills/my-skill/config.json
{
"required": ["apiKey"],
"properties": {
"apiKey": { "type": "string" }
}
}
## 步骤 4:添加配置
$ openclaw config set my-skill.apiKey "sk-xxx"
## 步骤 5:验证配置
$ openclaw skill config my-skill
{
"name": "my-skill",
"config": {
"apiKey": "sk-xxx"
}
}
## 问题解决
原因:缺少必填配置项 apiKey
解决:通过 config set 命令添加配置案例三:权限问题
问题描述:
用户:my-skill 报错 "Permission denied: browser-use"调试过程:
## 步骤 1:查看错误详情
$ grep "Permission" logs/error.log
[ERROR] Permission denied: skill 'my-skill' cannot access 'browser'
[ERROR] Required permission: browser-use
## 步骤 2:检查 SKILL.md
$ cat skills/my-skill/SKILL.md
---
name: my-skill
permissions: [] // 权限列表为空
---
## 步骤 3:添加权限声明
permissions: ["browser-use", "network-access"]
## 步骤 4:重新加载
$ openclaw skill reload my-skill
## 步骤 5:用户授权
检测到新权限请求:
- browser-use:使用浏览器
- network-access:网络访问
是否授权?[Y/n]: Y
## 问题解决
原因:未声明所需权限
解决:在 SKILL.md 中添加 permissions 声明调试最佳实践
1. 结构化日志
javascript
// 好的日志写法
logger.debug({
skill: 'my-skill',
action: 'fetch-data',
params: { url: 'https://api.example.com' },
duration: 150
});
// 差的日志写法
console.log('fetching data...');2. 错误处理
javascript
// 完整的错误处理
try {
const result = await fetchData();
return result;
} catch (error) {
logger.error({
skill: 'my-skill',
error: error.message,
stack: error.stack
});
throw new SkillError('数据获取失败', { cause: error });
}3. 单元测试
javascript
// tests/my-skill.test.js
describe('my-skill', () => {
it('should fetch data successfully', async () => {
const result = await mySkill.fetchData({ url: testUrl });
expect(result).toBeDefined();
});
it('should handle network error', async () => {
await expect(mySkill.fetchData({ url: invalidUrl }))
.rejects.toThrow('网络请求失败');
});
});调试清单
遇到问题时,按此顺序排查:
□ 1. 检查日志
□ 查看主日志:logs/openclaw.log
□ 查看错误日志:logs/error.log
□ 搜索关键词:error, fail, exception
□ 2. 检查状态
□ Skill 是否加载:openclaw skill status
□ 配置是否正确:openclaw skill config
□ 依赖是否完整:openclaw skill deps
□ 3. 检查权限
□ 是否声明所需权限
□ 用户是否授权
□ 4. 检查环境
□ 环境变量是否设置
□ 网络是否正常
□ 资源是否充足
□ 5. 性能分析
□ 是否超时
□ 内存是否充足
□ 是否有性能瓶颈总结
| 调试类型 | 工具/方法 | 关键命令 |
|---|---|---|
| 日志分析 | tail, grep | tail -f logs/openclaw.log |
| 状态检查 | skill status | openclaw skill status my-skill |
| 配置调试 | skill config | openclaw skill config my-skill |
| 性能分析 | profile, trace | openclaw profile --skill my-skill |
| 依赖检查 | skill deps | openclaw skill deps my-skill |
调试核心原则:
- 先看日志,再动手
- 一次只改一个变量
- 记录问题,避免重复踩坑
- 写单元测试,预防问题
掌握系统化的调试方法,让 Skill 开发效率提升 5 倍。关键是养成先看日志、再分析原因、最后动手修复的习惯。
