class Loggingmiddleware (object): def process_request (self, request): = time.time () def process_response (self, request, response): = Time.time ()- request.start_time = Request.get_full_path () info_logger.info ('request%s execute_time%f' % (path, execute_time)) return response
middleware_classes = ( 'django.middleware.common.CommonMiddleware', ' Mobile.middlewares.LoggingMiddleware ' ,)
1 middleware ensures that the process_request and Process_response methods do not have multithreading concurrency, so the request in 2 methods must be the same request
2 methods do not refer to global variables or variables in the current class, otherwise there will be problems in multithreaded concurrency or co-processes
3 Process_request is executed sequentially from top to bottom, process_response in turn
4 If the end of your urlconf has a backslash/, when the link does not have a backslash, commonmiddleware redirects the request to a URL with a backslash
5 There is a pit here, when the request is directed from Commonmiddleware, the process_request of all middleware behind it will not be executed, but the process_response of all middleware will be executed in reverse.
In this case, the Loggingmiddleware Process_response method above will be reported as abnormal, because there is no request.start_time
6 workaround, change the two middleware position, but this will record a request without a backslash (without a backslash request does not exist), there is a link all the requests are added backslashes, but for historical reasons
May sometimes be added to the whole.
Django Middleware records all requests and request execution times