To visualize your Loguru logs in Grafana, you'll need to set up a pipeline that collects, stores, and visualizes the logs. Here's a step-by-step guide using Loki (a lightweight log aggregation system designed for Grafana) and Promtail (Loki's log shipper):
1. Architecture Overview
code Copy code
Your App (Loguru logs) → Promtail (ships logs) → Loki (stores logs) → Grafana (visualizes logs)
2. Prerequisites
Docker and Docker Compose installed
Loguru logs written to ./log/*.log (e.g., ./log/app.log)
3. Set Up Loki, Promtail, and Grafana with Docker
Create a docker-compose.yml file:
yaml Copy code
version: '3.7'
networks:
loki:
services:
loki:
image: grafana/loki:latest
ports:
- "3100:3100"
volumes:
- ./loki-config:/etc/loki
command: -config.expand-env -config.file=/etc/loki/local-config.yaml
networks:
- loki
promtail:
image: grafana/promtail:latest
volumes:
- ./log:/logs # Mount your log folder
- ./promtail:/etc/promtail
command: -config.expand-env -config.file=/etc/promtail/config.yaml
ports:
- "12345:12345" # Optional: for metrics
depends_on:
- loki
networks:
- loki
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
volumes:
- ./grafana/provisioning:/etc/grafana/provisioning
depends_on:
- loki
networks:
- loki
4. Configure Loki and Promtail
a. ./loki-config/local-config.yaml (Loki config)
yaml Copy code
auth_enabled: false
server:
http_listen_port: 3100
storage_config:
boltdb_shipper:
active_index_directory: /loki/index
chunk_directory: /loki/chunks
schema_config:
configs:
- from: 2023-01-01
store: boltdb
object_store_id: filesystem
schema: v11
index:
prefix: index_
period: 24h
ruler:
rule_path: /loki/rules
alertmanager_url: http://localhost:9093
b. ./promtail/config.yaml (Promtail config)
yaml Copy code
server:
http_listen_port: 12345
positions:
filename: /tmp/positions.yaml
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
- job_name: "loguru-logs"
static_configs:
- targets:
- localhost
labels:
job: loguru
__path__: /logs/*.log # Path to your logs
5. Start the Stack
bash Copy code
docker-compose up -d
6. Configure Grafana
Open Grafana: http://localhost:3000
Add Loki as a data source:
Go to Configuration > Data Sources > Add data source
Select Loki
Set URL to http://loki:3100
Click Save & Test
Create a dashboard:
Go to Create > Dashboard
Add a new panel and use Loki queries like:
logql Copy code
{job="loguru"} |~ "ERROR|WARN" # Filter error/warning logs
Visualize logs in the Logs panel or build metrics with LogQL.
7. Verify Logs Are Flowing
Ensure Loguru writes logs to ./log/app.log (or similar).
In Grafana, check if logs appear under the Loki data source.
8. Optional Enhancements
Structured Logging : If Loguru outputs JSON logs, update Promtail's config to parse them:
yaml Copy code
pipeline_stages:
- json:
expressions:
level: level
message: message
Alerting : Configure Loki rules in ./loki/rules/ for alerts.
Retention : Adjust Loki's storage settings for long-term log retention.
9. Troubleshooting
Check Promtail/Pipeline errors: docker-compose logs promtail
Ensure file permissions allow Promtail to read ./log/*.log
Validate Loki ingesters: http://localhost:3100/metrics
With this setup, your Loguru logs will be automatically collected by Promtail, stored in Loki, and visualized in Grafana! 🎯