Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- See which labels exist — every query starts with a stream selector, so you need the label names and values:
- 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- Parse fields —
json,logfmt,patternorregexp, then filter on the fields:
logcli query '{job="nginx"} | pattern `<ip> - - <_> "<method> <path> <_>" <status> <size> <_>` | status >= 500' --since=1h- Show only what you need with
line_format:
logcli query '{app="api"} | json | level="error" | line_format "{{.ts}} {{.route}} {{.msg}}"' --since=1h- Follow live (like
tail -f):
logcli query --tail '{app="api"} |= "error"'- 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.
- 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.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| No results | Time range too short, or the label value is different. | --since=24h; check values with logcli labels NAME. |
parse error | Shell or LogQL quotes mixed up. | Single quotes around the whole query, double quotes inside, backticks for patterns. |
max entries limit / only 30 lines | Default limit. | --limit=1000 or narrow the time range. |
maximum number of series reached | High-cardinality labels. | Remove such labels at the shipper; parse at query time. |
401 Unauthorized | Loki behind auth or multi-tenant. | --username/--password or LOKI_USERNAME/LOKI_PASSWORD; --org-id. |
| Query is slow | Only a broad selector like {job=~".+"}. | Add precise labels and a line filter before the parser. |
Go further
- Command pages: logcli.
- Next how-tos: Ship logs with Fluent Bit, Query Azure logs with KQL, Find slow requests in Nginx logs, Build a Grafana dashboard, Read and follow the logs of a service.
- Learn it: Observability tools at work — Loki, Observability services — Loki. Full guide: Loki.
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.