Loki is a horizontally-scalable, highly-available log aggregation system designed to store and query logs from all your applications. Unlike traditional log systems that index full text, Loki indexes only metadata (labels) making it more efficient and cost-effective.
Description
Loki is like Prometheus but for logs. It uses the same principles:
- Pull model (Promtail pushes, Loki stores)
- Label-based indexing (not full-text search)
- LogQL query language (similar to PromQL)
In this stack, Loki:
- Receives logs from Promtail via HTTP push
- Stores log lines with labels
- Serves log queries via API
- Integrates with Grafana for visualization
The Loki configuration defines:
- Server settings (port, authentication)
- Storage settings (where to store log data)
- Schema configuration (how to organize data)
- Rate limits and retention
# =============================================================================# AUTHENTICATION# =============================================================================auth_enabled:false# Disable authentication for local development# =============================================================================# SERVER SETTINGS# =============================================================================server:http_listen_port:3100# HTTP API port# =============================================================================# COMMON SETTINGS# =============================================================================common:instance_addr:127.0.0.1# Instance address (localhost)path_prefix:/loki# Root directory for all Loki datastorage:filesystem:chunks_directory:/loki/chunks# Directory for compressed log datarules_directory:/loki/rules# Directory for recording rulesreplication_factor:1# 1 = single node (no replication)ring:kvstore:store:inmemory# Store ring info in memory# =============================================================================# SCHEMA CONFIGURATION# =============================================================================schema_config:configs:-from:2024-01-01# Start using this schema from this datestore:tsdb# Use TSDB for storageobject_store:filesystem# Store objects on local filesystemschema:v12# Schema version (latest)index:prefix:index_# Prefix for index filesperiod:24h# Rotate index every 24 hours# =============================================================================# LIMITS CONFIGURATION# =============================================================================limits_config:retention_period:30d# Keep logs for 30 daysingestion_rate_mb:16# Max ingestion rate (MB/s)ingestion_burst_size_mb:32# Max burst size (MB)
1. Promtail reads log file line
β
βΌ
2. Parse JSON (if JSON formatted)
Extract labels: app, level, logger
β
βΌ
3. Send to Loki
POST http://obs-loki:3100/loki/api/v1/push
β
βΌ
4. Loki receives and stores:
- Index: labels β chunk ID
- Chunk: actual log lines
β
βΌ
5. Available for querying
Log Query Flow
1. Grafana sends query to Loki
GET /loki/api/v1/query?query={app="django"}
β
βΌ
2. Loki uses index to find matching chunks
β
βΌ
3. Retrieve log lines from chunks
β
βΌ
4. Return to Grafana for display
# Push a test logcurl-XPOST'http://localhost:3100/loki/api/v1/push'\-H'Content-Type: application/json'\-d'{"streams":[{"stream":{"app":"test","level":"info"},"values":[["'$(date+%s)'000000000","test message"]]}]}'
Query Logs
# Query all logs for app=django (URL encoded)curl-s'http://localhost:3100/loki/api/v1/query?query=%7Bapp%3D%22django%22%7D'# Query with level filtercurl-s'http://localhost:3100/loki/api/v1/query?query=%7Bapp%3D%22django%22,level%3D%22ERROR%22%7D'# Query rangecurl-s'http://localhost:3100/loki/api/v1/query_range?query=%7Bapp%3D%22django%22%7D&start=1773532800000000000&end=1773536400000000000&limit=100'
Storage Inspection
# Check storage sizedockerexecobs-lokidu-sh/loki/
# List chunksdockerexecobs-lokils-la/loki/chunks/