django-structlog is a structured logging integration for Django projects that leverages the `structlog` library. It enriches logs with cohesive metadata, simplifying event and incident tracking. The current version is 10.0.0, and the library maintains an active release cadence with multiple updates throughout the year to support new Django and Python versions.
pip install django-structlogVerified import paths — ran on the pinned version, not inferred.
To get started with `django-structlog`, install the package, add `"django_structlog"` to your `INSTALLED_APPS`, and `"django_structlog.middlewares.RequestMiddleware"` to your `MIDDLEWARE` settings. Crucially, configure your `LOGGING` dictionary in `settings.py` to use `structlog.stdlib.ProcessorFormatter` with appropriate processors, including `structlog.contextvars.merge_contextvars` as the first processor. Finally, configure `structlog` itself using `structlog.configure()`. You can then obtain loggers using `structlog.get_logger()` and log structured messages.
Review custom exception handling within middleware or signal receivers to ensure compatibility with the new `got_request_exception` signal mechanism.
Update signal receiver functions to accept `log_kwargs` (e.g., `def my_receiver(request, logger, log_kwargs, **kwargs):`).
Update `structlog.configure` processors and replace `logger.bind` calls with `structlog.contextvars.bind_contextvars`. Consult the upgrade guide for detailed instructions.
Review `django-ipware`'s changelog for version 6 and adjust any custom IP address retrieval or handling logic if necessary.
If `user_id` is critical for all logs within a DRF authenticated request, consider explicitly binding it early in the request lifecycle using a custom signal receiver for `django_structlog.signals.bind_extra_request_metadata` or a custom middleware.
Refer to the 'Celery Integration' section in the official `django-structlog` documentation for complete setup instructions and recommended configurations.
Install the 'structlog' library using pip: `pip install structlog`.
Ensure your `LOGGING` dictionary in `settings.py` includes a logger for your application (e.g., `""` for the root logger or your specific app name) with the desired `level` (e.g., `DEBUG` or `INFO`), and set `disable_existing_loggers: False` if you intend to integrate with existing Django loggers. For example: `LOGGING = {..., 'loggers': {'': {'handlers': ['console'], 'level': 'INFO', 'propagate': True}, ...}}`.Ensure that `structlog.configure()` is called before any `structlog.get_logger()` calls are made in your application, ideally at the very end of your `settings.py` file, and that it correctly specifies `logger_factory=structlog.stdlib.LoggerFactory()` if you intend to integrate with Python's standard logging library.
Add `'django_structlog.middlewares.RequestMiddleware'` to your `MIDDLEWARE` list in `settings.py`. For `user_id` to be bound, ensure it's placed after `django.contrib.sessions.middleware.SessionMiddleware` and `django.contrib.auth.middleware.AuthenticationMiddleware`.
Replace calls to `logger.bind(...)` with `structlog.contextvars.bind_contextvars(...)` for setting context. Additionally, ensure `structlog.contextvars.merge_contextvars` is included as the first processor in your `structlog.configure()` call to ensure context variables are properly merged into log events.