Registry / web-framework / django-log-request-id

django-log-request-id

JSON →
library2.1.2pypypi✓ verified 86d ago

django-log-request-id is a Django middleware and log filter that automatically attaches a unique request ID to every log message generated as part of a web request. This greatly simplifies tracing and debugging in complex or distributed systems by allowing all related log entries for a single request to be easily correlated. The current version is 2.1.2, and the library maintains an active release cadence with frequent updates.

pip install django-log-request-id
INSTALL
IMPORT
SIG · DJANGO-LOG-REQUEST
D
django-log-request-id
web-frameworkpythonv2.1.2
Install
3.4s avg
Import
240ms
Disk
65MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.255s · 66.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.4s · import 0.225s · 67MB
65MB installed
● package 65MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RequestIDMiddleware
from log_request_id.middleware import RequestIDMiddleware
RequestIDFilter
from log_request_id.filters import RequestIDFilter

To integrate django-log-request-id, first add the `RequestIDMiddleware` to the top of your `MIDDLEWARE` setting. Then, define `RequestIDFilter` in your `LOGGING` configuration, and ensure your logging handlers use this filter and your formatters include `%(request_id)s` to display the unique ID.

import logging # settings.py # Add to MIDDLEWARE. It should be at the very top. MIDDLEWARE = [ 'log_request_id.middleware.RequestIDMiddleware', # ... other middleware ] # Configure LOGGING LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'request_id': { '()': 'log_request_id.filters.RequestIDFilter' } }, 'formatters': { 'standard': { 'format': '[%(asctime)s] [%(levelname)s] [%(request_id)s] %(name)s: %(message)s' } }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'standard', 'filters': ['request_id'] } }, 'loggers': { '': { 'handlers': ['console'], 'level': 'INFO', 'propagate': True }, 'django': { 'handlers': ['console'], 'level': 'INFO', 'propagate': False } } } # In your application code (e.g., views.py) logger = logging.getLogger(__name__) logger.info("This log message will include the request ID.")
Debug
Known issues
breakingVersion 2.0.0 removed the `requests.Session` passthrough functionality. Code relying on this feature will break when upgrading to 2.0.0 or later.
fix
Review your code for any reliance on `requests.Session` passthrough. If necessary, refactor to handle session management explicitly within your application logic.
affects: >=2.0.0
gotchaThe `RequestIDMiddleware` should be placed as high as possible in your `MIDDLEWARE` tuple, ideally at the very top, to ensure the request ID is available for all subsequent middleware and application logic. Incorrect placement may result in logs from earlier middleware not containing the request ID.
fix
Ensure `'log_request_id.middleware.RequestIDMiddleware'` is the first or one of the first entries in your `MIDDLEWARE` setting.
affects: All versions
gotchaPrior to version 2.1.0, logging user attributes via `LOG_USER_ATTRIBUTE` could inadvertently cause the session to be touched, potentially leading to unwanted side effects like setting a `Vary` header. Version 2.1.0 introduced `LOG_USER_ATTRIBUTE = None` to explicitly prevent user attribute logging and avoid session interaction.
fix
Upgrade to version 2.1.0 or later. If you do not wish to log user attributes, set `LOG_USER_ATTRIBUTE = None` in your Django settings. If you log user attributes, be aware of potential session side effects in older versions.
affects: <2.1.0
breakingVersion 1.7.0 dropped support for older, unsupported Django versions. Upgrading to 1.7.0 or later with an outdated Django installation may lead to incompatibilities and errors.
fix
Ensure your Django project uses a supported Django version (e.g., Django 3.0+ for 1.7.0). Always check the library's `requires_python` and Django compatibility matrix before upgrading.
affects: >=1.7.0
gotchaIf you rely on external systems (e.g., load balancers, proxies) to provide a request ID via a custom header, ensure your `LOG_REQUEST_ID_HEADER` setting in Django is correctly configured. The header name must conform to Django's `request.META` key format (e.g., `HTTP_X_REQUEST_ID` for an `X-Request-ID` header).
fix
Set `LOG_REQUEST_ID_HEADER` in your Django settings to the correct `META` key for your custom request ID header. For example, if your header is `X-My-Request-ID`, set `LOG_REQUEST_ID_HEADER = 'HTTP_X_MY_REQUEST_ID'`.
affects: All versions
Errors
Common errors & fixes
KeyError: 'request_id' OR AttributeError: 'LogRecord' object has no attribute 'request_id'
This error occurs when the logging formatter attempts to include `%(request_id)s` in a log message, but the `log_request_id.filters.RequestIDFilter` has not successfully injected the 'request_id' attribute into the LogRecord object.
fix
Ensure that `log_request_id.filters.RequestIDFilter` is correctly defined in your `LOGGING['filters']` setting and then explicitly applied to the relevant handlers in your `LOGGING['handlers']` configuration. For example: `LOGGING = {..., 'filters': {'request_id': {'()': 'log_request_id.filters.RequestIDFilter'}}, 'handlers': {'console': {'filters': ['request_id'], ...}}, ...}`
Request ID not appearing in logs
The request ID is not being generated or propagated to the logging system, often due to the `RequestIDMiddleware` not being placed correctly in the `MIDDLEWARE` setting or the logging filter/formatter being misconfigured.
fix
Verify that `log_request_id.middleware.RequestIDMiddleware` is placed at the very top of your `MIDDLEWARE` list in `settings.py` to ensure it captures every request. Additionally, confirm that the `log_request_id.filters.RequestIDFilter` is correctly configured and applied to your logging handlers and that your formatter includes `%(request_id)s`.
ModuleNotFoundError: No module named 'log_request_id'
The `django-log-request-id` package has not been installed in your project's environment, or the path to the middleware or filter in `settings.py` is incorrect.
fix
Install the package using pip: `pip install django-log-request-id`. After installation, double-check that the middleware path (`'log_request_id.middleware.RequestIDMiddleware'`) and filter path (`'log_request_id.filters.RequestIDFilter'`) are correctly specified in your `settings.py`.
Upgrade
Version history
2.1.2latest on PyPI · released Mar 12, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
28 hits · last 30 days
node
26
OpenAI (training)
2
Resources
django-log-request-id — pip install django-log-request-id · libregistry