{"id":5908,"library":"django-log-request-id","title":"Django Log Request ID","description":"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.","status":"active","version":"2.1.2","language":"en","source_language":"en","source_url":"https://github.com/dabapps/django-log-request-id/","tags":["django","logging","middleware","request-id","tracing"],"install":[{"cmd":"pip install django-log-request-id","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"RequestIDMiddleware","correct":"from log_request_id.middleware import RequestIDMiddleware"},{"symbol":"RequestIDFilter","correct":"from log_request_id.filters import RequestIDFilter"}],"quickstart":{"code":"import logging\n\n# settings.py\n# Add to MIDDLEWARE. It should be at the very top.\nMIDDLEWARE = [\n    'log_request_id.middleware.RequestIDMiddleware',\n    # ... other middleware\n]\n\n# Configure LOGGING\nLOGGING = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    'filters': {\n        'request_id': {\n            '()': 'log_request_id.filters.RequestIDFilter'\n        }\n    },\n    'formatters': {\n        'standard': {\n            'format': '[%(asctime)s] [%(levelname)s] [%(request_id)s] %(name)s: %(message)s'\n        }\n    },\n    'handlers': {\n        'console': {\n            'level': 'DEBUG',\n            'class': 'logging.StreamHandler',\n            'formatter': 'standard',\n            'filters': ['request_id']\n        }\n    },\n    'loggers': {\n        '': {\n            'handlers': ['console'],\n            'level': 'INFO',\n            'propagate': True\n        },\n        'django': {\n            'handlers': ['console'],\n            'level': 'INFO',\n            'propagate': False\n        }\n    }\n}\n\n# In your application code (e.g., views.py)\nlogger = logging.getLogger(__name__)\nlogger.info(\"This log message will include the request ID.\")","lang":"python","description":"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."},"warnings":[{"fix":"Review your code for any reliance on `requests.Session` passthrough. If necessary, refactor to handle session management explicitly within your application logic.","message":"Version 2.0.0 removed the `requests.Session` passthrough functionality. Code relying on this feature will break when upgrading to 2.0.0 or later.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure `'log_request_id.middleware.RequestIDMiddleware'` is the first or one of the first entries in your `MIDDLEWARE` setting.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Prior 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.","severity":"gotcha","affected_versions":"<2.1.0"},{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=1.7.0"},{"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'`.","message":"If 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).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}