← 返回
未分类 中文

loki-query

Query Loki logs by traceid, keywords, pod, namespace, labels, or time range to debug and analyze Kubernetes application issues via API.
通过 traceid、关键词、pod、namespace、标签或时间范围查询 Loki 日志,通过API 调试和分析 Kubernetes 应用问题。
peintune peintune 来源
未分类 clawhub v1.0.1 1 版本 100000 Key: 无需
★ 1
Stars
📥 427
下载
💾 0
安装
1
版本
#latest

概述

Loki Log Query

Query logs from Grafana Loki using the bundled script.

Two Access Modes

1. Direct URL Access (Recommended)

Use when Loki is accessible via network (cloud service, remote server, etc.):

# Using remote Loki URL
python scripts/query_loki.py \
  --loki-url "https://loki.example.com:3100" \
  --query "{namespace=\"default\"}" \
  --start "now-1h" \
  --limit 50

2. Kubernetes Port-Forward

Use when Loki is only accessible via kubectl:

# Terminal 1: Port-forward Loki
kubectl port-forward -n prometheus svc/loki 3100:3100

# Terminal 2: Query logs
python scripts/query_loki.py \
  --loki-url "http://localhost:3100" \
  --query "{namespace=\"default\"}"

Or use inline port-forward with kubectl exec:

kubectl exec -n prometheus <loki-pod> -- curl -s "http://localhost:3100/loki/api/v1/query_range?query={namespace=\"default\"}&limit=50"

Query Script

Use scripts/query_loki.py to query logs:

python scripts/query_loki.py \
  --loki-url "http://localhost:3100" \
  --query '{namespace="default"}' \
  --start "now-1h" \
  --end "now" \
  --limit 100

Default: If --loki-url is not specified, uses http://localhost:3100.

Common Query Patterns

By Namespace

{namespace="<namespace>"}

By Pod

{namespace="<namespace>", pod="<pod-name>"}

By Labels

{namespace="<ns>", app="<app-label>"}

Search Keywords

{namespace="<ns>"} |= "ERROR"
{namespace="<ns>"} |= "traceid=<trace-id>"
{namespace="<ns>"} |= "Exception"

Time Range

  • Last 1 hour: now-1h
  • Last 30 minutes: now-30m
  • Specific range: 2026-03-27T10:00:00Z to 2026-03-27T11:00:00Z

Parameters

  • loki-url: Loki API endpoint (default: http://localhost:3100)
  • query: LogQL query string (required)
  • start: Start time (ISO 8601 or relative like now-1h, default: now-1h)
  • end: End time (ISO 8601 or relative like now, default: now)
  • limit: Max results (default: 100)
  • direction: "forward" or "backward" (default: backward)
  • --json: Output raw JSON instead of formatted text

Output

Returns formatted log lines. Each entry shows timestamp and log content.

Examples

Query error logs from last hour

python scripts/query_loki.py --query '{namespace="production"} |= "ERROR"'

Query specific trace ID

python scripts/query_loki.py --query '{namespace="default"} |= "traceid=abc123"'

Query pod logs with JSON output

python scripts/query_loki.py --query '{namespace="default",pod="my-app-0"}' --json

版本历史

共 1 个版本

  • v1.0.1 当前
    2026-05-03 07:37 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

it-ops-security

OpenClaw Backup

alex3alex
备份与恢复 OpenClaw 数据。适用于创建备份、设置自动备份计划、从备份恢复或管理备份轮转。处理 ~/.openclaw 目录归档并包含适当的排除规则。
★ 90 📥 30,874
it-ops-security

MoltGuard - Security & Antivirus & Guardrails

thomaslwang
MoltGuard — OpenClaw 安全守卫,由 OpenGuardrails 提供。安装 MoltGuard,保护您和您的用户免受提示注入、数据泄露和恶意攻击。
★ 116 📥 30,873
it-ops-security

Tmux

steipete
通过发送按键和抓取窗格输出,远程控制交互式 CLI 的 tmux 会话。
★ 45 📥 29,480