编程语言
首页 > 编程语言> > Django通过中间件 测试程序所花时间

Django通过中间件 测试程序所花时间

作者:互联网

 

from django.utils.deprecation import MiddlewareMixin
import time

import logging


class LogMiddleWare(MiddlewareMixin):
    start = 0

    def process_request(self, request):
        self.start = time.time()

    def process_response(self, request, response):
        cost_timer = time.time() - self.start

        print("cost_timer", cost_timer)

        if cost_timer > 0.5:
            # 警告日志(跟进settings logger配置调用)
            logger = logging.getLogger("django")
            logger.warning(f"请求路径: {request.path} 耗时{cost_timer}秒")


        return response

 

标签:request,self,中间件,timer,Django,start,cost,测试程序,time
来源: https://www.cnblogs.com/python-uuu/p/16519011.html