There are a couple of ways this can be handled within django
Add a middleware to the django app that logs the request and all data you need from the request i.e request.POST
if it is a POST and request.GET
if it is a GET.
In a file called middleware.py
import logging
logger = logging.getLogger('app')
class RequestLoggingMiddleware:
def process_request(self, request):
logger.debug(request)
logger.debug('Logged Request')
return None
In your settings.py
, add middleware.RequestLoggingMiddleware
to the MIDDLEWARE_CLASSES
.
Somethings related to this are listed on Is there a Django middleware/plugin that logs all my requests in a organized fashion? and https://github.com/kylef/django-request
The other option is to add a log handler that logs requests that error out. The logging level can be changed to debug to log all requests
From official docs
django.request Log messages related to the handling of requests. 5XX
responses are raised as ERROR messages; 4XX responses are raised as
WARNING messages.
Messages to this logger have the following extra context:
status_code: The HTTP response code associated with the request.
request: The request object that generated the logging message.
Add the below handler to your log dict config file.
'django.request':
{
'handlers':
['mail_admins', 'console', 'file'],
'level':
'ERROR',
'propagate':
False,
},
Once you have setup logging requests, and have your logs being collected somewhere, there are different ways to analyze them. I use Logentries which collects my logs and provides an interface where I can filter by time and do a grep
like search. Sometimes when this is not adequate, I download the logs as a tar and use a version of Splunk running locally that has better search tools. But as long as you can filter by time and find the appropriate request logs, you should be able to debug what is happening.