Python SDK
Send structured logs from Python with logstack-py — Django, FastAPI, and stdlib logging capture.
Python SDK
The official Python client for Logstack. Batch shipping, stdlib logging capture,
and optional Django / FastAPI exception middleware.
| PyPI | logstack-py |
| Import | from logstack import LogStackClient |
| Version | 1.0.2 |
| Python | 3.7+ |
| Default API | https://api.logstack.tech |
| Ingest path | POST {api_url}/v1/logs |
Installation
Optional extras:
Quick start
Context manager (auto-close()):
Configuration
| Parameter | Default | Description |
|---|---|---|
api_key | — | Project ingest key |
api_url | https://api.logstack.tech | API host (SDK appends /v1/logs) |
environment | production | Environment label on each batch |
flush_interval | 5.0 | Background flush interval (seconds) |
batch_size | 100 | Max buffered entries before flush |
capture_logging | True | Auto-forward stdlib logging |
on_error | None | Callable[[Exception, list], None] on send failure |
Self-hosted:
Logging API
| Level | Method | Notes |
|---|---|---|
debug | debug | Verbose diagnostics |
info | info | Normal operations |
warn | warn | Recoverable issues |
error | error | Failures |
critical | critical | Severe failures |
fatal | fatal | Logs then flushes |
Explicit calls use source: "python-sdk". Auto-captured stdlib records use
source: "python-logging".
Flush / close
Stdlib logging capture
With capture_logging=True (default), a handler is installed on the root logger:
- All
logging.getLogger(…).info/error/…calls are forwarded - Levels map: CRITICAL→
critical, ERROR→error, WARNING→warn, INFO→info, elsedebug - Metadata includes
logger,pathname,lineno,funcNamewhen available - Your existing handlers (console, files) still receive records
- Internal
logstacklogger messages are skipped (no feedback loops)
Disable when you only want explicit client calls:
Django integration
Install extras and wire middleware:
DjangoMiddleware logs unhandled exceptions with path, method, user, IP, and
traceback. For a custom client instance:
Prefer explicit client.info/error in views for business events; use middleware
for unexpected failures.
FastAPI / Starlette integration
Recommended: create_fastapi_middleware(client):
FastAPIMiddleware(app, client=…) still works but is a deprecated
alias. Prefer create_fastapi_middleware.
Scripts and workers
Troubleshooting
| Symptom | Check |
|---|---|
| No logs in dashboard | Valid API key, network, on_error callback |
| Only some levels appear | Root logger level — SDK lowers to INFO when capture is on |
Double /v1 | Pass host only; normalize_api_url strips trailing /v1 |
| FastAPI import error | pip install "logstack-py[fastapi]" |
Logs after close() | Client drops new entries when closed |