Django Middleware records all requests and request execution times

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.