Log Management on a Budget: Loki + Grafana in Practice

Logs pile up fast. A busy site writes thousands of lines a day, and the moment something breaks, you need to find one line in that pile. Loki Grafana log management gives you a way to search those lines from a dashboard, without paying for a hosted log platform or running a heavy search cluster. This guide covers a small setup that fits on a single VPS: Loki to store logs, Promtail to collect them, and Grafana to search them.

Loki works like Prometheus for logs. It stores log lines cheaply and indexes only labels, such as the service name and the host, instead of indexing every word. That trade keeps storage small and the setup light, and it means you search with LogQL, a query language that filters by labels first and then by text.

Why Loki Instead of Elasticsearch

Elasticsearch is a strong tool, but it wants memory and disk for a full-text index of every log line. On a small VPS that index can cost more than the services you are logging. Loki skips the full-text index, so a 2 GB server can hold weeks of logs from a handful of services without strain.

The trade is speed on broad text searches. Loki answers fast when you filter by labels first, such as one service on one host, then search the text inside that stream. It answers slowly when you search all text across all services at once. For budget log management on a small server, that trade is usually fine, because most debugging starts with one service. Our comparison of the best log management tools puts Loki, Elasticsearch, and Datadog side by side if you want the full picture before you commit.

The Parts of the Stack

Three containers do the work. Promtail reads log files from disk and ships them to Loki with labels attached. Loki stores the lines and answers queries. Grafana draws the dashboard and runs LogQL queries. You can run all three on one server with Docker Compose, and none of them needs much memory.

Keep the stack on the same server as the services it watches while you learn it. Later you can move Loki to its own server so logs survive an app server failure.

Install Loki, Promtail, and Grafana with Docker Compose

Create a project directory and a Compose file that starts all three services. This grafana loki setup keeps Loki and Promtail on the internal Docker network, while Grafana gets a port you can reach through a reverse proxy.

mkdir -p /opt/loki-stack && cd /opt/loki-stack
cat > docker-compose.yml <<'EOF'
services:
  loki:
    image: grafana/loki:3.3.2
    container_name: loki
    restart: unless-stopped
    ports:
      - "127.0.0.1:3100:3100"
    volumes:
      - ./loki-config.yml:/etc/loki/local-config.yaml
      - loki-data:/loki
    command: -config.file=/etc/loki/local-config.yaml
  promtail:
    image: grafana/promtail:3.3.2
    container_name: promtail
    restart: unless-stopped
    volumes:
      - /var/log:/var/log:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - ./promtail-config.yml:/etc/promtail/config.yml
    command: -config.file=/etc/promtail/config.yml
  grafana:
    image: grafana/grafana:11.5.2
    container_name: grafana
    restart: unless-stopped
    ports:
      - "127.0.0.1:3000:3000"
    volumes:
      - grafana-data:/var/lib/grafana
volumes:
  loki-data:
  grafana-data:
EOF
docker compose up -d

Both web ports bind to localhost, so put Grafana behind a reverse proxy with HTTPS before you log in. Change the default admin/admin login first. Promtail points at http://loki:3100/loki/api/v1/push, and Loki uses the local filesystem for storage; start from the example configs in the Grafana docs and adjust the paths to match your server.

Connect Grafana to Loki

In Grafana, go to Connections, add a Loki data source, and set the URL to http://loki:3100. Save it and run a test query. If the test fails, check that the Loki container is running and that Grafana can reach it on the Docker network. A wrong URL is the most common cause of a failed first connection.

Once the data source works, open Explore, pick Loki, and run your first query. Try {job="varlogs"} to list system log lines, then narrow it down with a service label. If you already run Prometheus and Grafana for metrics, this is the same Grafana you know; our guide to building a Prometheus and Grafana monitoring stack covers the metrics side of that same dashboard.

What to Collect First

Start with the logs that answer the questions you actually ask when something breaks. System logs from /var/log/syslog show service restarts and kernel messages. Nginx access and error logs show which URLs returned 500 errors and when. Application logs from Docker containers show the errors your code wrote.

Label each stream with the service name, the host, and the environment, such as production or staging. Labels are the index Loki has, so a stream with no useful labels is hard to find later. Keep label values small and stable: a label with hundreds of values, such as a full URL path, makes queries slow and storage larger.

Loki Grafana Log Management: Your First LogQL Queries

LogQL looks strange at first, but a few loki logql queries cover most daily use. A label filter picks the stream, a text filter narrows the lines, and a pipe can extract numbers from the text.

{job="nginx"} |= "500"
{service="api"} |= "error" | json | level="error"
rate({job="varlogs"} |= "failed" [5m])
sum by (service) (count_over_time({job="nginx"} |= "500" [1h]))

The first query lists Nginx lines that mention 500. The second pulls error lines from the API service and parses them as JSON. The third shows the rate of failed lines over five minutes. The fourth counts 500 errors per service over the past hour. Save the queries you use more than once as Grafana dashboard panels so you do not retype them during an incident.

Build a Dashboard Worth Opening

A good log dashboard answers four questions at a glance: which services are writing errors, how many errors happened in the last hour, what the newest errors say, and whether the rate is rising. Build one panel for each question and keep the dashboard to a single screen.

Add a panel listing the latest error lines across all services, newest first, a time-series panel of error counts per service so a spike stands out, one for 500 responses from Nginx, and one for failed SSH logins. Resist the urge to add twenty panels: a dashboard nobody opens is worse than a small one you check every morning.

Retention and Disk Space on a Budget

Logs grow without limit unless you set one. Loki retention is set in the config file with a retention period, such as 30 days for a small server. Shorter retention keeps disk use low and queries fast, and most debugging uses logs from the past few days anyway.

Check disk use weekly until you know the growth rate: measure one day of storage, multiply by your retention days, and keep that much free plus a margin. If disk use climbs too fast, drop debug-level logs from noisy services first; dropping a chatty debug stream is usually worth more than adding disk.

Common Mistakes to Avoid

  • Too many labels: a label per request ID or per URL turns the index into a second copy of the logs. Keep labels to service, host, and environment.
  • Collecting everything: shipping every debug line from every container fills the disk with lines you will never read. Collect errors and warnings by default, add debug lines only for the service you are actively fixing.
  • No retention limit: without one, Loki grows until the disk is full and the whole server stops. Set retention on day one.
  • Grafana open to the internet: bind it to localhost, put it behind HTTPS, and use a strong password. Log dashboards show internal hostnames, paths, and sometimes secrets that leaked into a log line.

When to Move Beyond This Setup

This single-server setup carries a small blog, a few client sites, or a small SaaS through its first year. Move Loki to its own server when log volume grows past a few gigabytes a day, when you need logs from more than a few hosts, or when you need logs to survive an app server going down. At that point, add an object-storage backend so Loki can keep months of history without a large local disk.

Until then, keep the setup small and the queries saved. A Loki Grafana log management setup you actually open every day beats a large platform you never learned. If you run Kubernetes, the same ideas apply there too, and our guide to monitoring Kubernetes with Prometheus and Grafana shows the metrics side of that world.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.