Ultra-performant Loki logger for Python applications async (FastAPI, asyncio) and sync (scripts, Django, etc.) environments.
Project description
Loki Console Logger for Python 🚀
A high-performance console logger for Python that automatically sends all print, logging, unhandled exceptions, and custom events to Grafana Loki — powered by asyncio + aiohttp.
✅ Ideal for microservices, observability pipelines, cloud apps, and scripts.
🌀 Compatible with both async (FastAPI, asyncio) and sync (scripts, Django, etc.) environments.
✨ Features
- ⚡ Ultra-performant (fully async using
aiohttp) - 🧠 Dynamic + static labels support (e.g., env, hostname, region)
- 🧹 Batching + auto-flush for performance and throughput
- 🔗 Automatically captures:
print(...)callsloggingmessages- uncaught exceptions
- 🔒 Optional
Authorizationheader + multi-tenancy (X-Scope-OrgID) - 🧱 Zero external dependencies (only
aiohttp) - 🧘 Safe fallback for synchronous apps (auto flush on shutdown)
📦 Installation
pip install loki-console-logger-python
🚀 Quickstart (Async Example)
from loki_console_logger_python import LokiLogger
from loki_console_logger_python.config import LokiLoggerOptions
options = LokiLoggerOptions(
url="https://loki.elvenobservability.com/loki/api/v1/push",
tenant_id="your-tenant-id",
app_name="my-app",
auth_token="your-optional-token",
labels={"env": "production"},
dynamic_labels={"hostname": lambda: "my-host"},
)
logger = LokiLogger(options)
# Track custom events
logger.track_event("user_signup", {"user_id": 123})
# This will be captured automatically
print("✅ Hello from Python!")
# This will also be captured
import logging
logging.info("App started")
# So will uncaught exceptions
raise RuntimeError("Simulated failure")
🧱 Example with FastAPI
from fastapi import FastAPI
from loki_console_logger_python import LokiLogger
from loki_console_logger_python.config import LokiLoggerOptions
app = FastAPI()
logger = LokiLogger(LokiLoggerOptions(
url="https://loki.elvenobservability.com/loki/api/v1/push",
tenant_id="elven",
app_name="fastapi-app",
labels={"env": "staging"},
dynamic_labels={"hostname": lambda: "api-node-1"},
))
@app.get("/ping")
def ping():
print("Ping received!")
logger.track_event("ping", {"status": "ok"})
return {"message": "pong"}
🐍 Example with Synchronous Script
from loki_console_logger_python import LokiLogger
from loki_console_logger_python.config import LokiLoggerOptions
logger = LokiLogger(LokiLoggerOptions(
url="https://loki.elvenobservability.com/loki/api/v1/push",
tenant_id="elven",
app_name="data-script",
labels={"env": "batch"},
))
print("Running ETL script...")
try:
raise ValueError("Unexpected value")
except Exception:
pass
logger.track_event("script_end", {"status": "completed"})
# Optional: force flush for long-running tasks
logger.flush_sync()
⚙️ Configuration Options
| Option | Description | Default |
|---|---|---|
url |
Full Loki Push API URL | — |
tenant_id |
Value for X-Scope-OrgID (multi-tenancy) |
— |
app_name |
Used as a label in logs |
— |
auth_token |
Optional Bearer token | None |
batch_size |
Max logs before flush | 10 |
flush_interval |
Seconds between auto-flush | 2 |
labels |
Fixed labels (e.g., env, service) | {} |
dynamic_labels |
Runtime-evaluated labels (e.g., hostname) | {} |
🤝 Contributing
Contributions are very welcome — new features, bug fixes, or ideas.
Let's make observability in Python awesome together! 🛠️
- Fork the repository
- Create a new branch (
git checkout -b feat/my-feature) - Open a pull request
📄 License
Licensed under the MIT License.
Created with ❤️ by Leonardo Zwirtes
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file loki_console_logger_python_2-1.0.1.tar.gz.
File metadata
- Download URL: loki_console_logger_python_2-1.0.1.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1b7e21666dbcf7d02c5335d7fa85b8efe11296eb18a91f5703cdde675ede246
|
|
| MD5 |
564edefb0fdfd90537a550f2ff91a488
|
|
| BLAKE2b-256 |
fc675c55fec48725d411d4f7a2e47151fc24ff0e2bd892ed8a09a957420ebc6a
|
File details
Details for the file loki_console_logger_python_2-1.0.1-py3-none-any.whl.
File metadata
- Download URL: loki_console_logger_python_2-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9685891bf59ae60058543fc457e1a05fafbb2655930aa382579727c987cec301
|
|
| MD5 |
197e44106ece4e2df57b05cc8b4143f4
|
|
| BLAKE2b-256 |
e7bad46a3a243d65f6db3da6d810d774259b9eef35559e11836b1cbab3076a1f
|