django-structlog
Structured Logging for Django
What it is and what it does
django-structlog is a middleware and signal-based integration layer that adds structured logging to Django applications. It automatically captures request context—request ID, user ID, IP address—and binds it to every log entry within that request's lifecycle, making logs queryable and traceable. Instead of flat text logs, you get rich metadata in JSON or key-value formats that can be searched, aggregated, and correlated across your application.
The package works by installing a middleware component that generates a unique request ID and extracts user information from the request, then uses structlog's context-variable binding to attach this metadata to all logs emitted during request processing. It supports Django REST Framework and django-ninja out of the box, and provides signal hooks to bind additional custom metadata. Logs can be rendered as JSON for log aggregation systems, flat key-value lines for grep-friendly searching, or console output for development.
Use it for:
- Track requests end-to-end by searching logs with a request ID across multiple services or log files.
- Correlate user actions with errors and performance issues by having user_id automatically included in every log.
- Aggregate and analyze logs in JSON format using tools like jq or centralized logging platforms (ELK, Datadog, etc.).
- Debug multi-user applications by filtering logs by IP address or user ID without manual context passing.
- Extend logging metadata with custom request attributes (domain, tenant ID, feature flags) via signal receivers.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Integrates structured logging into Django applications using structlog, automatically capturing request metadata like request IDs, user IDs, and IP addresses in each log entry.
Yes. django-structlog is actively maintained, has no known vulnerabilities, uses a permissive MIT license, and low install friction. It solves a real problem—correlating logs across requests—that becomes critical as Django applications grow beyond single-server deployments. If you need queryable, structured logs with automatic request context binding, this is a straightforward addition to your Django stack.
Install
django-structlog on PyPI
pip
pip install django-structloguv
uv add django-structlogpoetry
poetry add django-structlogInstalling django-structlog
Before you install
Low install friction with a pure Python wheel distribution. The package is actively maintained with a recent release, and depends only on django, structlog, asgiref, and django-ipware—all standard, well-established libraries.
License in practice
MIT license is permissive; you can use, modify, and distribute this package freely in commercial and open-source projects with minimal restrictions.
Quickstart
pip install django-structlog
# In settings.py INSTALLED_APPS:
INSTALLED_APPS = ["django_structlog"]
# In settings.py MIDDLEWARE:
MIDDLEWARE = ["django_structlog.middlewares.RequestMiddleware"]
# Then log with structlog:
import structlog
logger = structlog.get_logger(__name__)
logger.info("event_name", key="value")
Requires Python 3.10 or later; structlog configuration in Django settings.py is necessary for output formatting (JSON, key-value, or console).
Verify before relying
- Whether Celery task logging integration requires significant additional setup beyond what the documentation covers.
- Performance impact of automatic request metadata binding on high-throughput Django applications.
- Compatibility with async Django views and ASGI servers beyond the asgiref dependency.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — django, structlog, asgiref, django-ipware |
| Maintenance | actively maintained — 76 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 3,749,882/month — #2,505 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: django_structlog-10.1.0-py3-none-any.whl
Tags
More Logging packages
Wraps any iterable to display a real-time…
copyleft · top 100 on PyPI
python-json-loggerFormats Python logging output as JSON, making…
permissive · top 1,000 on PyPI
structlogstructlog is a structured logging library that…
permissive · top 1,000 on PyPI
loguruLoguru provides a pre-configured logger that…
permissive · top 1,000 on PyPI
coloredlogsAdds colored output to Python's standard…
permissive · top 1,000 on PyPI
wandbwandb is a machine learning experiment tracking…
permissive · top 1,000 on PyPI
structlog-sentryBridges structlog and Sentry to automatically…
permissive · top 5,000 on PyPI
macwinnie-enhanced-loggingProvides a batteries-included logging setup…
unclear · top 15,000 on PyPI
structlog-gcpConfigures structlog to output logs in Google…
unclear · top 15,000 on PyPI
pytest-structlogProvides a pytest fixture for capturing and…
permissive · top 15,000 on PyPI
django-log-request-idAttaches a unique request ID to every log…
permissive · top 15,000 on PyPI
structlog-prettyAdds structlog processors that format log…
permissive · top 15,000 on PyPI
django-request-idAttaches a unique request ID to each Django…
permissive · top 15,000 on PyPI
opentelemetry-instrumentation-structlogIntegrates structlog logging with OpenTelemetry…
permissive · top 15,000 on PyPI
daiquiriDaiquiri simplifies Python logging…
permissive · top 15,000 on PyPI