Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
Wat het doet
Grafana Loki is "Prometheus for logs": agents (Grafana Alloy, Promtail, Fluent Bit) send log lines with labels (namespace, app, host); Loki indexes only the labels and keeps the text compressed in object storage (Azure Blob, S3). You search with LogQL: a label selector {app="shop"} plus filters (|= "error") and parsers (| json). You use it in Grafana's Explore view or with logcli.
Zo schrijf je het
logcli [--addr=URL] query 'LOGQL' [--since=1h] [--limit=N] [--tail]
logcli labels [NAME]Belangrijkste delen
Commands
query'LOGQL'- Search logs.
instant-query'LOGQL'- Metric query at one time.
labels[NAME]- List labels / values of one label.
series'SELECTOR'- List streams.
stats'SELECTOR'- Size of the data.
Opties 7
Meest gebruikt
Flags
Niets past bij dit filter.
Voorbeelden 10
$ als gewone gebruiker# als root (of met sudo)
1Set the address
export LOKI_ADDR=http://loki.monitoring.svc:3100 logcli labels logcli labels namespace2Find errors of one app
logcli query '{namespace="shop", app="api"} |= "error"' --since=1h --limit=503Follow logs live
logcli query '{namespace="shop"}' --tail4Parse JSON logs and filter by a field
logcli query '{app="api"} | json | status >= 500 | line_format "{{.method}} {{.path}} {{.status}}"' --since=30m5Count errors per app (metric query)
logcli instant-query 'sum by (app) (count_over_time({namespace="shop"} |= "error" [5m]))'6Find failed SSH logins on all hosts
logcli query '{job="systemd-journal", unit="sshd.service"} |~ "Failed password|Invalid user"' --since=24h7Exclude noise
logcli query '{app="ingress-nginx"} != "/health" != "/metrics"' --since=15m8Save raw lines to a file
logcli query '{app="api"}' --since=2h --limit=5000 -o raw > api.log9Install Loki with Helm (single binary, lab)
10Alert on log lines (Loki ruler)
cat rules/shop.yml groups: - name: shop-logs rules: - alert: ManyErrors expr: sum(rate({namespace="shop"} |= "error" [5m])) > 1
Niets past bij dit filter.
Let op
- Do not use high-cardinality labels (user ID, request ID); put them in the text and filter.
- Logs can contain personal data and secrets — set retention and limit who can query.
Bestanden
/etc/loki/config.yaml- Loki server config (path can differ).
Tips om te onthouden
- In Grafana, link logs to traces and metrics with the same labels (
app,namespace).
Exitcodes
0 | Success. |
1 | Error. |