Logging requests to django-rest-framework

后端 未结 8 691
迷失自我
迷失自我 2020-12-02 12:31

For debugging purposes, I would like to use Django\'s logging mechanism to log each and every incoming request when it \"arrives\" at django-rest-framework\'s doorstep.

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 13:04

    In new Django 2+ better to use Middleware as a callable object like this, just connect it with your project in your Middlewares section of the settings.py file (also Middleware can be a function, not only a class because it's a callable object old MiddlewareMixin now in the deprecated module of Django):

    More info in docs:
    https://docs.djangoproject.com/en/2.2/topics/http/middleware/#writing-your-own-middleware

    class UserActivityLogMiddleware:
        def __init__(self, get_response):
            self.get_response = get_response
    
        def __call__(self, request):
            print(request.method)    # In this string we catch request object.
            response = self.get_response(request)
            return response
    

提交回复
热议问题