I want to be able to log all REST requests that arrive at the Django REST framework API. I want to log the response of API calls too. Where can I define my function that logs all REST API requests and responses?
Asked
Active
Viewed 3,732 times
5
-
Ideally we will need to log request and response of an api call together. Having separate process_request and process_response will not handle this. – Kumar Deepak Aug 21 '19 at 10:40
-
https://stackoverflow.com/questions/57589907/how-to-log-request-and-response-for-4xx-http-status-in-django-rest-framework – Kumar Deepak Aug 21 '19 at 12:05
2 Answers
2
You can write a Django middleware component to intercept traffic into and out of the application.
There will be two methods, process_response
and process_request
. Use each one to log the response and request, respectively.
class LoggingMiddleware:
def process_request(self, request):
# do your logging here
def process_response(self, request, response):
# do your logging here
But this will log all requests and responses, not only to the specific API. If you want to log only API ones, you can easily check the URL in this method for prefix like /api
and log info based on this.
-
Thanks for your response. I did the following and got 500 error.. I'm not sure what is wrong. class MyMiddleware(): def process_request( self, request ):
return Response( status=status.HTTP_200_OK ) def process_response( self, request, response ): – linuxfreak May 31 '16 at 09:01return Response( status=status.HTTP_200_OK ) Do you find anything amiss? -
1it should return None not a Response if u want only logging. And whats is in logs? – Aldarund May 31 '16 at 12:33
-
Thanks for your answer. The code works. class MyMiddleware(): def process_request( self, request ): print "xxxxx" return None def process_response( self, request, response ): print "xxxxx" return response – linuxfreak Jun 01 '16 at 08:49
-
The code works fine. But, the request.user doesn't have the actual username. As per my code, the user is populated only after hitting the authentication classes. As I see, the middleware classes are called before authentication classes. Are there any set of classes that are called after authentication and from which I can log the REST api? – linuxfreak Jun 17 '16 at 09:10
-
1@linuxfreak u just need to move this middleware below auth middleware in the list – Aldarund Jun 17 '16 at 16:04
-1
This depends on your how you want to log the information and how you have build your API.
For something simple try looking into logging
Your API must take some sort of input of the query, so wrap that in a log
callAPI(query)
logging.info(query)
and the same with your API return
return APIJSONanswer
logging.info(APIJSONanswer)

MadsVJ
- 678
- 6
- 19
-
I'm familiar with logging module. But, my point is I don't want to call logging.info in each viewset. If there is a common function that gets called before the call is routed to each viewset, I can just call logging.info there. – linuxfreak May 30 '16 at 11:36
-
There should be. It depends on your build though. I can't get more specific before I know more about you setup. – MadsVJ May 30 '16 at 11:40
-
Is there a common function in django framework that gets hit before going to a viewset? – linuxfreak May 30 '16 at 11:45