This is a complete, hands-on Grafana Loki cheat sheet: install, configure, operate, automate and secure it - with copy-paste commands grouped by task. Bookmark it for day-to-day work, incidents and interviews alike.
Loki is a horizontally scalable log system that indexes only labels (not the full log line), making it far cheaper than traditional log stores and a natural fit next to Prometheus and Grafana.
Why you reach for it
- Low-cost logging by indexing labels instead of full text
- LogQL mirrors PromQL, so metrics and logs feel consistent
- Tight Grafana integration for correlated dashboards
Installation
docker run -d --name loki -p 3100:3100 grafana/loki:latest
docker run -d --name promtail -v $PWD/promtail.yaml:/etc/promtail/config.yml grafana/promtailConfiguration reference
Promtail scraping local logs and shipping to Loki.
server: { http_listen_port: 9080 }
clients: [{ url: http://loki:3100/loki/api/v1/push }]
scrape_configs:
- job_name: system
static_configs:
- targets: [localhost]
labels: { job: varlogs, __path__: /var/log/*log }Core commands you use daily
LogQL queries for filtering and metrics from logs.
{job="varlogs"} |= "error" | logfmt | status >= 500
# turn logs into a rate metric
sum(rate({app="web"} |= "panic" [5m]))LogQL quick reference
{app="web"} |= "error" # substring match
{app="web"} | json | status >= 500 # parse + filter
sum(rate({app="web"} |= "panic" [5m])) # logs -> metric
{app="web"} | logfmt | latency > 1slogcli
logcli labels
logcli query '{app="web"} |= "error"' --since=1h --limit=100
logcli series '{app="web"}'Use it in CI/CD
Query logs programmatically with logcli.
logcli query '{app="web"} |= "error"' --since=1h --limit=100Best-practice checklist
- Keep labels low-cardinality; filter within the stream using LogQL
- Set per-tenant retention and ingestion limits
- Structure logs (logfmt/JSON) so LogQL parsers work well
Common pitfalls to avoid
- High-cardinality labels (like request_id) destroying performance
- Pushing huge log volumes without retention or limits
- Expecting full-text indexing - Loki filters, it doesn't index content
Security quick wins
- Enable multi-tenancy and per-tenant limits
- Scrub secrets/PII before shipping logs
Works well with
- Grafana - The open dashboarding layer for metrics, logs and traces, with alerting across many data sources.
- Prometheus - The de-facto standard for metrics - a pull-based time-series database with a powerful query language.
- Elasticsearch - A distributed search and analytics engine for logs, full-text search and (now) vectors.
A cheat sheet is muscle memory on paper - keep it close until the commands live in your fingers.
Want senior help putting Grafana Loki to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.