实战

OpenClaw 日志与健康检查完全指南:监控、告警与运维自动化

OpenClaw 日志系统(Logging)与健康检查(Health Check)完整教程:日志级别配置(debug/info/warn/error)和日志格式(text/json)、日志文件持久化路径配置、按渠道/Agent/Provider 过滤日志、Health Check HTTP 端点(/health)的使用(状态码/响应格式)、用于容器编排的 liveness/readiness 探针配置、Gateway Doctor 命令的详细输出解读、集成 Prometheus 指标导出(/metrics 端点)、Grafana Dashboard 可视化,以及生产环境的日志轮转和告警配置方案。

2026/3/253分钟 阅读ClaudeEagle

生产环境的 OpenClaw 需要完善的可观测性—— 日志、健康检查、指标,这些是运维稳定性的基础。

日志配置

基础日志设置

json
{
  "logging": {
    "level": "info",
    "format": "json",
    "file": "/var/log/openclaw/gateway.log"
  }
}

日志级别(从详细到简洁):

  • debug:所有调试信息(开发用,不要在生产环境开)
  • info:正常运行信息(推荐生产环境默认)
  • warn:警告(潜在问题,不影响运行)
  • error:错误(影响功能的问题)

JSON 格式日志(推荐生产环境)

json
{
  "logging": {
    "level": "info",
    "format": "json",
    "file": "/var/log/openclaw/gateway.log",
    "rotate": {
      "maxSize": "100mb",
      "maxFiles": 7
    }
  }
}

JSON 格式输出示例:

json
{"level":"info","ts":"2026-03-25T14:23:01Z","channel":"telegram",
 "userId":"@alice","tokens":{"input":1234,"output":456},"latencyMs":1823}

便于 Elasticsearch/Loki 等日志系统解析。

按组件分级

json
{
  "logging": {
    "level": "info",
    "components": {
      "gateway": "info",
      "channels.telegram": "debug",
      "channels.slack": "warn",
      "providers.anthropic": "info",
      "exec": "debug"
    }
  }
}

可以对特定渠道开启 debug,其他保持 info,精准排查问题。

命令行查看日志

bash
# 实时日志流
openclaw logs --follow

# 最近 100 行
openclaw logs --tail 100

# 按级别过滤
openclaw logs --level error
openclaw logs --level warn

# 按渠道过滤
openclaw logs --channel telegram
openclaw logs --channel slack

# 搜索关键词
openclaw logs --grep "rate_limit"
openclaw logs --grep "403"

# 时间范围
openclaw logs --since 30m
openclaw logs --since "2026-03-25 14:00"

# 导出
openclaw logs --since 1h > /tmp/debug.log

Health Check 端点

Gateway 提供标准 HTTP 健康检查接口:

bash
# 基础健康检查
curl http://127.0.0.1:18789/health

# 正常响应(200 OK)
{
  "status": "ok",
  "version": "1.5.0",
  "uptime": 86400,
  "channels": {
    "telegram": "connected",
    "slack": "connected",
    "matrix": "disconnected"
  },
  "providers": {
    "anthropic": "ok",
    "deepseek": "ok"
  }
}

# 异常响应(503 Service Unavailable)
{
  "status": "degraded",
  "errors": ["anthropic: api key invalid"]
}

Kubernetes 探针配置

yaml
# deployment.yaml
livenessProbe:
  httpGet:
    path: /health
    port: 18789
  initialDelaySeconds: 30
  periodSeconds: 30
  failureThreshold: 3

readinessProbe:
  httpGet:
    path: /health/ready
    port: 18789
  initialDelaySeconds: 10
  periodSeconds: 10

/health:存活检查(进程是否运行) /health/ready:就绪检查(所有渠道是否连接成功)

Prometheus 指标

json
{
  "metrics": {
    "enabled": true,
    "path": "/metrics",
    "port": 9090
  }
}

关键指标:

# 请求计数(按渠道/Agent/状态) openclaw_requests_total{channel="telegram",agent="default",status="success"} 1234 # 响应延迟(P50/P95/P99) openclaw_latency_seconds{quantile="0.95"} 2.3 # Token 使用量 openclaw_tokens_total{provider="anthropic",type="input"} 234567 # 活跃会话数 openclaw_sessions_active 42 # 错误计数 openclaw_errors_total{type="rate_limit"} 5

Prometheus 配置:

yaml
# prometheus.yml
scrape_configs:
  - job_name: 'openclaw'
    static_configs:
      - targets: ['localhost:9090']
    scrape_interval: 30s

Grafana Dashboard

配置 Prometheus 数据源后,导入 OpenClaw 官方 Dashboard:

Grafana → + → Import → 输入 Dashboard ID(见官网) Dashboard 面板包括: - 实时请求量(按渠道分布) - Token 消耗趋势(日/周/月) - 响应延迟 P95 趋势 - 错误率告警图 - 模型使用分布

告警配置

Prometheus AlertManager

yaml
# alerts.yml
groups:
  - name: openclaw
    rules:
      - alert: HighErrorRate
        expr: rate(openclaw_errors_total[5m]) > 0.1
        annotations:
          summary: "OpenClaw 错误率过高"

      - alert: APIKeyExpired
        expr: openclaw_errors_total{type="auth_error"} > 0
        annotations:
          summary: "API Key 认证失败,请检查密钥"

简单告警(OpenClaw 内置)

json
{
  "alerts": {
    "errorRate": {
      "threshold": 10,
      "window": "5m",
      "channel": "telegram"
    },
    "providerDown": {
      "channel": "telegram",
      "message": "AI 服务商连接异常,已自动切换备用模型"
    }
  }
}

来源:OpenClaw 官方文档 - docs.openclaw.ai/gateway/logging

相关文章推荐

实战OpenClaw 代理配置完全指南:SOCKS5/HTTP 代理接入 Claude API 解决网络限制OpenClaw 网络代理(Proxy)配置完整教程:为什么需要代理(大陆访问 Anthropic/OpenAI API 被限制)、SOCKS5 代理配置方式(proxy.socks5/proxy.url)、HTTP/HTTPS 代理配置、代理认证(带用户名密码的代理)、按 Provider 单独配置代理(Anthropic 用代理、国内模型不走代理)、Clash/V2Ray/Xray 等代理工具与 OpenClaw 的对接方式、代理连通性测试方法,以及常见代理问题排障(SSL证书错误/超时/认证失败)。2026/3/25实战OpenClaw 密钥管理完全指南:API Key 安全存储、环境变量与 Vault 集成OpenClaw 密钥(Secrets)管理完整教程:密钥存储的三种方式对比(配置文件明文/环境变量/外部 Vault)、openclaw secrets set/get/list 命令使用、环境变量在配置中的引用语法(${ENV_VAR})、与系统 Keychain 集成(macOS Keychain/Linux Secret Service)、1Password CLI 和 HashiCorp Vault 接入方案、密钥轮换的操作流程、防止密钥泄露的检查(避免 git commit 含密钥)、以及密钥的最小权限原则(每个渠道用独立的 Token)。2026/3/25实战OpenClaw 渠道排障完全指南:消息收不到、Bot 不回复的系统性诊断方法OpenClaw 渠道故障系统性诊断教程:openclaw doctor 一键诊断命令的输出解读、最常见的 5 类问题(Bot Token 无效/Webhook URL 不可达/DM 配对未完成/网络防火墙拦截/配置格式错误)及对应修复步骤、各主要渠道的专项排障(Telegram 403/WhatsApp QR 失效/Slack 事件订阅未开启/Discord 权限不足/Matrix E2EE 设备未验证)、Gateway 日志的关键字段解读、常见错误码含义(401/403/409/429/503),以及在 Discord 社区获取技术支持的途径。2026/3/25实战OpenClaw 费用控制完全指南:Token 限制、Rate Limit 与 API 成本优化实践OpenClaw API 费用控制完整教程:每请求/每日 Token 上限配置(maxTokensPerRequest/maxTokensPerDay)、Rate Limit 限流防刷设置、每日美元预算告警(budgetAlert)、模型降级策略(高峰期自动切 Haiku 降成本)、Prompt Caching 开启减少重复 Token 消耗、各模型每百万 Token 价格对比表、Ollama 本地模型 0 成本方案,以及监控 Token 用量的 Dashboard 和日志方法。2026/3/25实战OpenClaw 与 Claude Code 协同使用实战:AI 聊天助手 + AI 编程助手的终极组合OpenClaw 与 Claude Code 协同使用的完整实战指南:两款工具的定位差异(OpenClaw=聊天AI助手框架,Claude Code=代码库直接操作的编程工具)、在 OpenClaw 中通过 exec 工具调用 Claude Code CLI(claude 命令)执行编程任务、把 OpenClaw 的 Telegram 消息转化为 Claude Code 任务(用自然语言描述→Claude Code执行→返回结果)、使用 OpenClaw Cron 定期触发 Claude Code 执行代码审查/依赖更新/测试/文档生成、CRS 代理在两者中的统一接入方案,以及常见的协同架构模式(主动触发/被动响应/定时执行)。2026/3/24实战OpenClaw Trusted Proxy 反向代理认证完全指南:Nginx/Caddy 前置部署最佳实践OpenClaw Trusted Proxy 认证模式完整教程:为什么需要反向代理(HTTPS 证书/域名绑定/统一认证入口)、trusted-proxy 认证模式的工作原理(Nginx/Caddy 负责认证,通过 X-Forwarded-User 头传递身份给 Gateway)、Nginx + Basic Auth 配置示例、Caddy + JWT 配置示例、Cloudflare Access + OpenClaw 的零信任部署方案、让反向代理与 Gateway 之间的通信保持安全(内网绑定/Unix Socket)、常见错误排查(401 循环/WebSocket 升级失败/Underscores in Headers 问题)。2026/3/24