Vibe monitoring with Last9 MCP: Ask your agent to fix production issues! Setup →
Last9 Last9

Getting Started with Loki for Log Management

A practical guide to setting up Loki for logs—how it works, how to query, and what to watch out for in real-world environments.

May 21st, ‘25
Getting Started with Loki for Log Management
See How Last9 Works

Unified observability for all your telemetry.Open standards. Simple pricing.

Talk to us

Logs are essential, but managing them can be tedious. They quickly consume storage, slow down your searches, and make troubleshooting feel like an endless chore. Loki monitoring helps simplify this process, offering a more efficient approach to logging that developers can appreciate.

What Exactly Is Loki?

Loki is an open-source log management tool created by Grafana Labs. If you're familiar with Prometheus for metrics, Loki offers something similar—but specifically for logs. The biggest difference from traditional logging tools is that Loki doesn't index every single word in your logs; it indexes only metadata (labels). This design keeps it lightweight, fast, and cost-efficient.

A simple analogy can help: Traditional logging tools act like librarians who meticulously read and document every detail of each book in the library. Loki, on the other hand, just catalogs books by author, title, and genre, and doesn't open any book until you ask for it. When you need specific log details, Loki quickly finds and retrieves exactly what's needed, without the heavy overhead.

💡
If you're also comparing tracing tools to go alongside your logging setup, here's a look at how Grafana Tempo stacks up against Jaeger.

Why Care with Loki?

Loki brings a few practical benefits that developers care about:

It’s budget-friendly

Loki doesn't index every word in your logs, making it significantly lighter on storage and compute resources. If you've been feeling the pain of escalating log management costs, Loki can help keep your expenses in check.

Grafana integration is easy

If Grafana is already your go-to dashboard, adding Loki is straightforward. No complicated integration, no constant tool-switching. Logs and metrics coexist neatly, which makes troubleshooting faster and easier.

The query language isn't intimidating

Loki uses LogQL, which shares similarities with Prometheus's PromQL. If you already know PromQL, you're practically set. Even if you don't, LogQL has a gentle learning curve.

Quick to set up

Loki’s architecture is intentionally straightforward. There's no need for elaborate configurations or a weekend buried in documentation. For smaller teams, or anyone who just wants to get started quickly, this is a major advantage.

Scales when you need it

As your logging needs grow, Loki scales horizontally. You can add more capacity easily, without rethinking your entire logging infrastructure.

How Loki Logging Works

Loki takes a slightly different approach to logging, and understanding how it works can help you get more out of it.

The building blocks of Loki

Loki is made up of three parts that work together to collect, store, and query logs:

  • Promtail – log collector
    Promtail runs on your servers or containers. It reads log files, adds labels like app, environment, or log leveland sends those logs to Loki.
  • Loki server – storage and indexing
    Loki stores the logs it receives and indexes only the labels — not the full content of each log line. This keeps things simpler and reduces how much storage and compute you need.
  • Grafana – the interface
    Grafana connects to Loki and gives you a way to search, filter, and view logs. It’s where most engineers end up when debugging something or piecing together what went wrong.
💡
If you're using Prometheus with Grafana, this guide to the rate() function can help you interpret counter metrics more effectively.

How logs move through Loki

Here’s the general flow:

  1. Your apps generate logs like usual—stdout, log files, whatever you're already using.
  2. Promtail collects those logs, adds labels to describe where they came from, and forwards them to Loki.
  3. Loki stores the logs and builds an index based on the labels (not the log contents).
  4. Grafana queries Loki, using those labels to help you find the logs you’re looking for.

Why Loki relies on labels

Labels are the core of how Loki organizes logs. These are simple key-value pairs like:

app=payment-service
environment=production
instance=pod-3
level=error

Instead of scanning every log line, Loki uses labels to quickly find what you asked for. Since most log searches follow this pattern anyway (“show me error logs from this service in production”), labels keep things fast and manageable — especially when log volume grows.

Loki Step-by-Step Guide: Local and Kubernetes Options

Loki isn’t difficult to set up, and you don’t need a complex cluster or hours of configuration to get started. Here’s how to run it locally using Docker, or inside a Kubernetes cluster using Helm.

Option 1: Running Loki Locally with Docker

This setup is great if you want to try Loki on your laptop or test it in a dev environment.

Step 1: Create a folder for config files

mkdir loki-config
cd loki-config

Step 2: Download the default config

wget https://raw.githubusercontent.com/grafana/loki/main/cmd/loki/loki-local-config.yaml -O loki-config.yaml

This gives you a working config file that Loki will use when it starts up.

Step 3: Start Loki

docker run -d --name loki \
  -v $(pwd):/mnt/config \
  -p 3100:3100 \
  grafana/loki:latest \
  --config.file=/mnt/config/loki-config.yaml

Loki should now be running at http://localhost:3100.

Step 4 (Optional): Start Promtail to send logs

docker run -d --name promtail \
  -v $(pwd):/mnt/config \
  -v /var/log:/var/log \
  grafana/promtail:latest \
  --config.file=/mnt/config/promtail-config.yaml

Promtail reads logs from your system (e.g., /var/log/syslog) and forwards them to Loki. You'll need a Promtail config file as well (promtail-config.yaml) that tells it what to watch.

Option 2: Deploying Loki in Kubernetes (Using Helm)

If you’re already running a Kubernetes cluster, Helm is the simplest way to install Loki and its dependencies.

Step 1: Add the Grafana Helm repository

helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
helm install loki grafana/loki-stack --set grafana.enabled=true

This installs:

  • Loki (the log store)
  • Promtail (to collect logs from your pods)
  • Grafana (for querying and dashboards)

You can tweak this setup later with a values.yaml file, but the default works fine to start with.

💡
If you're setting up observability in Kubernetes, this guide on deploying the OpenTelemetry Helm chart can help you get started efficiently: Getting Started with the OpenTelemetry Helm Chart in K8s.

Connecting Loki to Grafana

Once Loki is running—whether locally or in a cluster—you’ll want to hook it up to Grafana so you can view your logs.

Here’s how:

  1. Open Grafana in your browser.
  2. Go to Settings → Data Sources.
  3. Click Add data source and choose Loki.
  4. Set the URL to:
    • http://localhost:3100 if you're running Loki locally via Docker
    • http://loki:3100 if you're in Kubernetes and Loki is running as a service
  5. Click Save & Test

Grafana will confirm the connection, and from there, you’re ready to start querying logs using labels and time ranges.

💡
Now, fix production Loki log issues instantly—right from your IDE, with AI and Last9 MCP. Bring real-time production context—logs, metrics, and traces—into your local environment to troubleshoot faster and with more clarity.

Loki Query Patterns for Common Troubleshooting Tasks

Once Loki is set up, the real value comes from how quickly you can find what you need.

Here are some patterns to get familiar with.

1. View all logs from a specific service

Start with a basic query to show logs from one source:

{job="auth-service"}

This gives you everything from auth-service. Good for general inspection or when you're not sure what you're looking for yet.

Looking at multiple services?

{job=~"auth-service|payment-api"}

The =~ operator lets you match with regular expressions. Useful when tracking interactions across services.

2. Filter logs by time window

Want to see what was going wrong in the last 30 minutes?

{job="auth-service"} |~ "error" [30m]

This grabs logs from the auth-service job that contain the word “error” within the past 30 minutes.

You can swap in [1h], [10m], etc., depending on how far back you want to look.

3. Search for specific patterns in logs

Case-insensitive error search:

{job="auth-service"} |~ "(?i)error"

The (?i) makes it ignore case—helpful when logs aren’t consistent with casing.

Need to find 500-level HTTP responses in NGINX logs?

{job="nginx"} |~ "HTTP/1\\.[01]\" [5]\\d\\d "

This looks for log lines that include a 500-series HTTP response. You can adjust the pattern for 4xx errors or other status codes too.

4. Count matching logs over time

Sometimes you don’t want the logs themselves—just a count. For example, to see how many errors occurred in the last 5 minutes:

count_over_time({job="auth-service"} |~ "error"[5m])

This is handy when creating alerts or monitoring error trends over time.

5. Parse JSON logs and filter by field

If your logs are structured (e.g., in JSON format), Loki lets you extract fields:

{job="auth-service"} | json | response_time > 200

Here, we extract the response_time field and return only logs where it’s above 200ms. You can combine this with other filters to narrow down performance issues.

How does Loki stack up against other options?

Different teams care about different things—some need full-text search, others want something lightweight that won’t chew up resources. Here’s a side-by-side comparison of Loki, ELK, Prometheus, and Last9 to help you see where each fits.

Feature Loki Traditional ELK Prometheus Last9
Primary Use Logs Logs Metrics Logs, Metrics, Alerts
Cost Low Medium–High Low Low
Setup Simple Requires more tuning Moderate Simple
Query Language LogQL Elasticsearch DSL PromQL LoqQL
Full-text Search No Yes No Yes
Resource Usage Low High Medium Low
Metrics Support Via Grafana Basic Native Native
Handles High Cardinality Can struggle Often a bottleneck Limited Designed for it

Loki is solid if you want something simple that integrates with Grafana and doesn’t require a lot of tuning.
ELK gives you full-text search and flexible queries, but comes with higher costs and more moving parts.
Prometheus is best for metrics, not logs, though it pairs well with Loki.
Last9, combines logs, metrics, and alerts with native support for high cardinality and low overhead.

Last9 Review
Last9 Review

Common Loki Issues and How to Fix Them

Even with a solid setup, Loki can run into a few common problems, most of which come down to how it’s configured or queried. Here’s a quick guide to what typically goes wrong and what you can do about it.

Queries feel slow or unresponsive

Possible cause: Too many unique label combinations (high cardinality).

What to check:

  • Are you using labels like request_id, user_id, or dynamic pod names? Move those into the log body instead.
  • Stick to stable, query-relevant labels like job, env, and level.
  • Run label_values() in Grafana to identify problematic labels.

Query patterns are inefficient

Possible cause: You're scanning too much data or using heavy regex filters up front.

What helps:

  • Narrow the time range before running the query.
  • Filter by labels first, then apply text searches.
  • Where possible, use line_format to display fields instead of relying on regex parsing.

Disk usage is growing fast

Possible cause: Loki is holding on to logs for too long or ingesting too much.

How to manage it:

  • Set a retention policy (retention_period) that fits your use case—7 or 14 days is usually enough.
  • Tune chunk size and flush settings (max_chunk_age, chunk_idle_period) to avoid memory pressure.
  • Use compression settings for storage backends.
  • Drop noisy logs (like /healthz, debug output) using relabel_configs in Promtail.

Logs aren’t showing up in Loki

Possible cause: Something’s off with your log pipeline.

Checklist:

  • Check your Promtail config—make sure the right files and paths are defined.
  • Confirm that Promtail has read permissions on the logs.
  • Make sure your labels are consistent and not misconfigured.
  • Look at Promtail’s logs for dropped entries or rate limits.

Running Loki in Production: Practical Best Practices

Once Loki is in place, it’s easy to forget that poor label choices or noisy logs can slow things down over time. These practices can help keep things fast, predictable, and easier to work with.

1. Keep your labels under control

Loki only indexes labels, so what you choose to label matters a lot.

Use labels like job, env, app, and level — the ones you usually filter by. Avoid adding things like request_id, user_id, or dynamic pod names. These create high-cardinality combinations that make queries slower and increase memory use.

If you're using Promtail with Kubernetes, trim down the default labels. Not all of them are useful, and some can add unnecessary overhead. It's also worth checking label_values() in Grafana now and then to spot anything that looks off.

2. Don’t let logs pile up

Just because Loki can handle a lot of logs doesn’t mean you should send everything.

Use log levels properly. Debug logs are fine in dev, but usually don’t belong in production unless you're tracking down a specific issue. Use relabel_configs in Promtail to drop routine noise — things like health checks, static file requests, or background jobs that run constantly.

Also, make sure you’re rotating and compressing log files. Loki isn’t designed to deal with logs that grow forever on disk.

💡
Understanding log levels is key to making your Loki setup useful—this guide breaks them down with clear, real-world examples.

3. Set up retention and chunk settings early

By default, Loki keeps everything unless you tell it not to.

Set a retention period — even 7 days is enough for many teams. If you’re using filesystem or object storage, tune how long log chunks sit in memory (chunk_idle_period) and when they get flushed (max_chunk_age). These help reduce memory usage and make logs show up faster in queries.

4. Build dashboards that don’t overload Loki

It’s easy to create slow dashboards without realizing it.

Keep your default time ranges short — 15 or 30 minutes is usually enough. Avoid wide regex filters like job=~".*" unless you need them. They’re expensive and can slow down queries a lot.

Limit how many log lines each panel returns. Most of the time, 100 lines is more than enough. And if things start feeling sluggish, split logs and metrics into separate panels instead of cramming everything into one view.

5. Write alerts that focus on patterns, not noise

A single “error” line usually isn’t alert-worthy. Instead, watch for patterns, like a sudden spike in timeouts or repeated failures over a short window.

Use queries like:

count_over_time({job="api"} |~ "timeout"[5m]) > 10

This catches repeated issues without waking someone up over a one-off log. And if you're alerting, include a few matching log lines for context — it saves time when someone's on call.

💡
If you're dealing with noisy logs or high-cardinality data, Last9 Alert Studio can help. It's built to handle these kinds of alerting challenges, reducing noise and making it easier to catch real issues without getting overwhelmed.

Wrapping Up

Loki offers a clean, efficient way to handle logs—label-based filtering, minimal setup, and smooth integration with Grafana. It’s a solid choice for teams that want log visibility without dealing with heavyweight infrastructure.

But logs alone don’t give you the full picture. That’s why we built Last9—a platform that brings together logs, metrics, and alerts, all tuned for high-cardinality systems. It’s built to reduce the operational burden that usually comes with managing observability at scale.

If you’re using Loki and want to go further — or just want to avoid stitching together half a dozen tools, check out Last9.

FAQs

Is Loki meant to replace ELK?
Not exactly. Loki takes a different approach—it doesn’t index the full log content, just metadata. That makes it more cost-efficient, but less flexible for full-text searches. Some teams run both: ELK for detailed search, Loki for lower-cost, label-based logging.

How does Loki handle a high volume of logs?
Loki is designed to scale horizontally. You can add more ingestors to handle incoming logs and more queriers to support search traffic. Storage scales independently, so it can grow with your needs.

Can I use Loki without Grafana?
Yes, but it’s not ideal. Loki exposes an HTTP API you can query directly, but Grafana makes it much easier to browse logs, build dashboards, and spot issues. Most setups use them together.

What’s the best way to structure logs for Loki?
Use JSON if you can. It makes filtering and extracting fields much easier. Also, be mindful about what you put in labels—only include values you’ll query on, and avoid high-cardinality stuff like user IDs or request IDs.

How is Loki different from Prometheus?
Prometheus is for metrics—numbers over time. Loki is for logs—text records. They’re often used side by side, and both work well with Grafana.

Can Loki collect logs from any source?
Pretty much. While Promtail is the default agent, Loki also supports Fluentd, Fluent Bit, Logstash, and Vector. You can pull logs from files, journald, Docker containers and Kubernetes pods.

How long are logs stored?
That depends on your retention settings. Loki lets you define how long different types of logs are kept—maybe 3 days for debug logs, 30 days for error logs. It’s flexible based on what you need to keep.

Authors
Anjali Udasi

Anjali Udasi

Helping to make the tech a little less intimidating. I love breaking down complex concepts into easy-to-understand terms.

Contents

Do More with Less

Unlock high cardinality monitoring for your teams.