Django Request Logging
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+.
Common errors
-
Logs are not appearing in the console/file for requests.
cause The `django.request` logger is not properly configured in Django's `LOGGING` settings, or `propagate` is set to `False` without an attached handler.fixVerify 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`. -
Logs are duplicated in the console or file.
cause The `django.request` logger is propagating messages to a parent logger that also has a handler configured.fixSet `propagate: False` for the `django.request` logger in your `LOGGING` settings to prevent message duplication if a parent logger also processes these messages. -
Logs are too verbose or not verbose enough.
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.fixAdjust `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. -
Logs appear without color, even in the console.
cause Colorization is disabled by default or explicitly turned off, especially if logging to a file.fixEnsure `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.
Warnings
- gotcha Sensitive data might be logged by default. The middleware logs request and response bodies.
- gotcha Incorrect Django `LOGGING` configuration can lead to missing or duplicated logs.
- gotcha Extensive request logging, especially to a database, can impact application performance.
- gotcha 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.
Install
-
pip install django-request-logging
Imports
- LoggingMiddleware
from request_logging.middleware import LoggingMiddleware
- no_logging
from request_logging.decorators import no_logging
Quickstart
import logging
# settings.py
INSTALLED_APPS = [
# ...
# 'request_logging', # Not strictly required, but can be added if you want to explicitly signal app presence
# ...
]
MIDDLEWARE = [
# ... other middleware
'request_logging.middleware.LoggingMiddleware',
# ... other middleware
]
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.request': {
'handlers': ['console'],
'level': 'DEBUG', # Change debug level as appropriate
'propagate': False, # Important: prevent duplication if other loggers handle 'django'
},
},
'root': {
'handlers': ['console'],
'level': 'WARNING',
}
}
# Optional: Customize log levels for specific HTTP status codes
# REQUEST_LOGGING_HTTP_4XX_LOG_LEVEL = logging.WARNING
# REQUEST_LOGGING_DATA_LOG_LEVEL = logging.INFO
# REQUEST_LOGGING_ENABLE_COLORIZE = False # Disable color for file logging
# Example usage in a view to disable logging
from django.http import HttpResponse
from request_logging.decorators import no_logging
@no_logging()
def sensitive_view(request):
return HttpResponse("This view's requests/responses are not logged.")
@no_logging(log_headers=False, log_body=False)
def partially_sensitive_view(request):
return HttpResponse("This view logs only metadata, no headers or body.")