Skip to main content...
Observability: Metrics, Logs, Traces
20 min

Day 103: Loki: LogQL and structured logging

Loki: logs, indexed like Prometheus indexes metrics

Loki deliberately does NOT index the full text of every log line (unlike Elasticsearch) — it indexes only labels (service, environment, level), keeping the log content itself compressed and cheap to store. You query with LogQL, which filters by label first (cheap), then optionally greps/filters the actual log content within that already-narrowed set.

A LogQL query
{service="api", environment="production"} |= "timeout" | json | duration > 5s

Structured logging

A plain string log line (Request failed for user 42) is hard to query reliably. Structured logging emits logs as JSON with consistent fields ({ "event": "request_failed", "userId": 42, "durationMs": 340 }) — trivially filterable, aggregable, and joinable with other structured data, at the small cost of being less human-readable when eyeballed raw.

Structured logging from a NestJS service
logger.error({
  event: 'request_failed',
  userId: 42,
  path: '/api/orders',
  durationMs: 340,
  error: err.message,
});

Key terms

LogQL
Loki's query language — filters by labels first, then log content.
Structured logging
Emitting logs as consistent, machine-parseable data (typically JSON) rather than free-form text.

Why is Loki's "index labels only, not full text" approach a deliberate trade-off rather than a limitation?

We use cookies

We use cookies to enhance your browsing experience, serve personalized content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies. Learn more

    Day 103: Loki: LogQL and structured logging | RBTechIconX