{"id":8955,"library":"django-request-logging","title":"Django Request Logging","description":"Django Request Logging is a middleware for Django projects that provides intuitive and color-coded logging of HTTP request and response payloads, suitable for both web and API requests. It integrates with Django's standard logging system, allowing developers to monitor traffic and debug applications effectively. The library is currently active, with version 0.7.5 released in February 2022, and supports Django 1.8+.","status":"active","version":"0.7.5","language":"en","source_language":"en","source_url":"https://github.com/Rhumbix/django-request-logging.git","tags":["Django","logging","middleware","HTTP","request","response","API"],"install":[{"cmd":"pip install django-request-logging","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The primary middleware to add to your Django MIDDLEWARE settings.","symbol":"LoggingMiddleware","correct":"from request_logging.middleware import LoggingMiddleware"},{"note":"Decorator to disable logging for specific views, especially useful for sensitive data.","symbol":"no_logging","correct":"from request_logging.decorators import no_logging"}],"quickstart":{"code":"import logging\n\n# settings.py\n\nINSTALLED_APPS = [\n    # ...\n    # 'request_logging', # Not strictly required, but can be added if you want to explicitly signal app presence\n    # ...\n]\n\nMIDDLEWARE = [\n    # ... other middleware\n    'request_logging.middleware.LoggingMiddleware',\n    # ... other middleware\n]\n\nLOGGING = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    'handlers': {\n        'console': {\n            'class': 'logging.StreamHandler',\n        },\n    },\n    'loggers': {\n        'django.request': {\n            'handlers': ['console'],\n            'level': 'DEBUG', # Change debug level as appropriate\n            'propagate': False, # Important: prevent duplication if other loggers handle 'django'\n        },\n    },\n    'root': {\n        'handlers': ['console'],\n        'level': 'WARNING',\n    }\n}\n\n# Optional: Customize log levels for specific HTTP status codes\n# REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL = logging.WARNING\n# REQUEST_LOGGING_DATA_LOG_LEVEL = logging.INFO\n# REQUEST_LOGGING_ENABLE_COLORIZE = False # Disable color for file logging\n\n# Example usage in a view to disable logging\nfrom django.http import HttpResponse\nfrom request_logging.decorators import no_logging\n\n@no_logging()\ndef sensitive_view(request):\n    return HttpResponse(\"This view's requests/responses are not logged.\")\n\n@no_logging(log_headers=False, log_body=False)\ndef partially_sensitive_view(request):\n    return HttpResponse(\"This view logs only metadata, no headers or body.\")\n","lang":"python","description":"To quickly set up `django-request-logging`, add `request_logging.middleware.LoggingMiddleware` to your `MIDDLEWARE` list in `settings.py`. Crucially, configure your Django `LOGGING` settings to handle the `django.request` logger. This logger is where `django-request-logging` outputs its messages. Setting `propagate: False` for `django.request` prevents log messages from being handled by parent loggers and potentially duplicated. You can also use the `@no_logging` decorator on views to control logging behavior for sensitive endpoints."},"warnings":[{"fix":"Use the `@no_logging` decorator on views handling sensitive data. For example, `@no_logging()` will disable all logging for that view, while `@no_logging(log_headers=False, log_body=False)` allows partial logging.","message":"Sensitive data might be logged by default. The middleware logs request and response bodies.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure a handler is defined for the `django.request` logger in your `LOGGING` dictionary. Set `propagate: False` for `django.request` to prevent logs from being processed multiple times by other handlers.","message":"Incorrect Django `LOGGING` configuration can lead to missing or duplicated logs.","severity":"gotcha","affected_versions":"All versions"},{"fix":"While `django-request-logging` primarily logs to `StreamHandler` (console), be mindful of log levels. Consider reducing the `level` for `django.request` in production (e.g., to `INFO` or `WARNING`) or using filters to log only critical requests to avoid excessive I/O.","message":"Extensive request logging, especially to a database, can impact application performance.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Adjust the `REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL` and `REQUEST_LOGGING_DATA_LOG_LEVEL` settings in `settings.py` to match your desired logging verbosity for different response types. For example, `REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL = logging.WARNING`.","message":"The default log levels for different HTTP status codes might be unexpected. By default, 4xx-5xx responses are logged at ERROR level, others at INFO/DEBUG.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify that `LOGGING['loggers']['django.request']` exists, has a handler (e.g., 'console'), and its `level` is set appropriately (e.g., 'DEBUG'). Also, ensure `propagate: False` is used correctly with a handler for `django.request`.","cause":"The `django.request` logger is not properly configured in Django's `LOGGING` settings, or `propagate` is set to `False` without an attached handler.","error":"Logs are not appearing in the console/file for requests."},{"fix":"Set `propagate: False` for the `django.request` logger in your `LOGGING` settings to prevent message duplication if a parent logger also processes these messages.","cause":"The `django.request` logger is propagating messages to a parent logger that also has a handler configured.","error":"Logs are duplicated in the console or file."},{"fix":"Adjust `LOGGING['loggers']['django.request']['level']` to control general verbosity. Use `REQUEST_LOGGING_DATA_LOG_LEVEL` and `REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL` in `settings.py` to fine-tune data and error log levels respectively.","cause":"The log level for the `django.request` logger is set too high or too low, or specific settings like `REQUEST_LOGGING_DATA_LOG_LEVEL` are not configured.","error":"Logs are too verbose or not verbose enough."},{"fix":"Ensure `REQUEST_LOGGING_ENABLE_COLORIZE = True` (which is the default) in your `settings.py`. If logging to a file, remember that ANSI color codes might not be desired in file output.","cause":"Colorization is disabled by default or explicitly turned off, especially if logging to a file.","error":"Logs appear without color, even in the console."}]}