Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 177Observability · Loki

How to search logs with Loki and LogQL

Loki stores logs cheaply because it indexes only labels (job, host, namespace) and not the text. A LogQL query first picks streams by label, then filters lines and parses fields, and can even turn logs into numbers for graphs and alerts. This how-to searches from the terminal with logcli (the same queries work in Grafana Explore): find errors, parse JSON and access logs, follow live, and count errors per status code.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

177.1

The quick answer

export LOKI_ADDR=http://loki.example.com:3100
logcli labels job
logcli query '{job="nginx"} |= "error"' --since=1h --limit=50
logcli query '{app="api"} | json | status >= 500' --since=30m

177.2

Step by step

  1. See which labels exist — every query starts with a stream selector, so you need the label names and values:
logcli labels
logcli labels host
logcli series '{job="nginx"}' --since=1h
  1. Line filters after the selector, fastest first: |= contains, != does not contain, |~ regex, !~ not regex:
logcli query '{job="nginx", host="web01"} != "healthcheck" |~ "(?i)timeout|refused"' --since=2h
  1. Parse fields — json, logfmt, pattern or regexp, then filter on the fields:
logcli query '{job="nginx"} | pattern `<ip> - - <_> "<method> <path> <_>" <status> <size> <_>` | status >= 500' --since=1h
  1. Show only what you need with line_format:
logcli query '{app="api"} | json | level="error" | line_format "{{.ts}} {{.route}} {{.msg}}"' --since=1h
  1. Follow live (like tail -f):
logcli query --tail '{app="api"} |= "error"'
  1. Turn logs into numbers — errors per status in 5-minute windows, or the 95th percentile of a duration field:
logcli instant-query 'sum by (status) (count_over_time({job="nginx"} | pattern `<_> "<_>" <status> <_>` [5m]))'
logcli instant-query 'quantile_over_time(0.95, {app="api"} | json | unwrap duration_ms [5m]) by (route)'

The same metric queries work as Loki ruler alerts or in Grafana panels.

  1. Keep labels few — never put user IDs, request IDs or IPs in labels (too many streams); keep them in the text and parse at query time.

177.3

Check that it worked

logcli labels job lists your jobs; the error query returns lines with time stamps and labels; the count_over_time query returns one number per status code that matches the access log.

177.4

Common problems

ProblemCauseFix
No resultsTime range too short, or the label value is different.--since=24h; check values with logcli labels NAME.
parse errorShell or LogQL quotes mixed up.Single quotes around the whole query, double quotes inside, backticks for patterns.
max entries limit / only 30 linesDefault limit.--limit=1000 or narrow the time range.
maximum number of series reachedHigh-cardinality labels.Remove such labels at the shipper; parse at query time.
401 UnauthorizedLoki behind auth or multi-tenant.--username/--password or LOKI_USERNAME/LOKI_PASSWORD; --org-id.
Query is slowOnly a broad selector like {job=~".+"}.Add precise labels and a line filter before the parser.

177.5

Go further

1Why does Loki only index labels?

Indexing only a few labels keeps storage small and cheap; the text is searched at query time inside the chosen streams.

2Why should a request ID never be a label?

Every new value makes a new stream; millions of streams make Loki slow and expensive.

Alle commando's in dit hoofdstuk