CLI 目录 横向对比 FCRS-1.0 标准 /llms.txt
← 返回 CLI 目录

Grafana LogCLI

$ logcli 官方出品 低风险
Grafana Labs · 办公协作与 SaaS · AGPL-3.0-only · macos / linux / windows

Grafana LogCLI(logcli)通过命令行直接向自建 Loki 或 Grafana Cloud Logs 发起 LogQL 查询。凭借 --output=jsonl / -o raw 模式、--quiet 静默开关以及 LOKI_* 环境变量配置,非常适合自动化 SRE 日志分诊。

FCRS-1.0 综合评分
84 /100
良好 (Good)
中置信度 (源码/文档)
零摩擦安装命令
brew install logcli
CLI vs MCP 选型建议 优先选 CLI

流式拉取或管道过滤大规模 LogQL 日志(-o jsonl / -o raw)优先用 logcli;若需跨面板或 Prometheus 指标联动可搭配 Grafana MCP。

Agent 友好度体检单

✓ 原生 JSON 输出
— 字段裁剪
✓ 无头非交互
— 支持 Dry-Run
— 只读模式开关
— 支持 NO_COLOR
✓ 自省/Schema 发现
— 官方 SKILL.md

当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。

FCRS-1.0 六维评分拆解

Agent 友好度 21 / 25

支持 --output=jsonl、-o raw、--quiet 与 --limit,并可通过 LOKI_* 环境变量无头运行。

  • --output=jsonl emits Loki API payloads as JSON Lines
  • -q/--quiet suppresses query progress noise on stderr/stdout
安全与权限边界 23 / 25

纯只读查询工具,支持 LOKI_ORG_ID 租户隔离与 --limit 数量上限,几乎无写操作风险。

  • Read-only query interface against Loki HTTP API
  • Supports read-only bearer tokens and basic auth env vars
安装与运行摩擦 13 / 15

单文件 Go 二进制,支持 Homebrew、go install、Docker 与 GitHub Releases。

  • brew install logcli and cross-platform Loki release binaries
能力覆盖与边际价值 12 / 15

非常适合调试 LogQL、探索标签以及将日志管道输出给 jq 或 rg。

  • Supports instant queries, range queries, and label/series inspection
维护与来源背书 8 / 10

由 Grafana Labs 在 grafana/loki 官方仓库随主版本以 AGPL-3.0-only 协议维护。

  • Maintained alongside Loki core releases
文档与可验证性 7 / 10

Grafana Loki 官方文档覆盖核心用法,具体参数有时需结合 logcli query --help 核对。

  • Official docs at grafana.com/docs/loki/latest/query/logcli/

安全边界、鉴权与放权指南

鉴权方式
无需鉴权 环境变量密钥 本地配置文件
凭证存储位置

从环境变量或命令行参数读取 LOKI_ADDR、LOKI_USERNAME、LOKI_PASSWORD、LOKI_BEARER_TOKEN 与 LOKI_ORG_ID;本地自建 Loki 可无需认证。

潜在副作用
只读查询
费用与配额提示

CLI 开源免费(AGPL-3.0-only);Grafana Cloud Logs 提供免费额度,超出后按日志量计费。

可复制到 CLAUDE.md / AGENTS.md 的安全护栏
# Grafana LogCLI Agent Rules
- Always pass `--limit=N` and `--since` (default `1h`) on `logcli query` to avoid dumping massive log streams.
- Use `-o jsonl --quiet` (or `-o raw --quiet`) for clean stdout parsing.
- Do not invoke `logcli query --tail` synchronously without a bounded timeout.

Agent 高频安全命令配方

只读查询(安全推荐)

列出 Loki 日志标签键
logcli labels --quiet

从已配置的 Loki 端点列出可用的日志标签名。

以 JSON Lines 查询近期错误日志
logcli query --output=jsonl --quiet --limit=50 '{app="api"} |= "error"'

从 api 应用查询最多 50 条匹配的错误日志并输出为 JSONL。

核心优势

  • + 纯只读查询定位使 SRE 分诊 Agent 的操作风险极低。
  • + --output=jsonl --quiet 与 -o raw 可顺畅接入 jq 等 UNIX 管道。
  • + 全环境变量配置(LOKI_ADDR、LOKI_BEARER_TOKEN)契合无头 CI 环境。

注意事项与踩坑点

  • ! logcli query --tail 会持续阻塞等待新日志直到超时终止。
  • ! 采用 AGPL-3.0-only 协议,二次分发网络服务时需留意许可证条款。

已验证兼容的 Agent 宿主

Claude Code Codex CLI Gemini CLI