Ai
1 Star 0 Fork 355

tzone/Django

forked from 极客时间/Django 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
performance.py 1.91 KB
一键复制 编辑 原始数据 按行查看 历史
import time
import logging
from django.http import HttpResponse
import traceback
from sentry_sdk import capture_exception
from . import dingtalk
logger = logging.getLogger(__name__)
def performance_logger_middleware(get_response):
def middleware(request):
start_time = time.time()
response = get_response(request)
duration = time.time() - start_time
response["X-Page-Duration-ms"] = int(duration * 1000)
logger.info("%s %s %s", duration, request.path, request.GET.dict() )
return response
return middleware
class PerformanceAndExceptionLoggerMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
start_time = time.time()
response = self.get_response(request)
duration = time.time() - start_time
response["X-Page-Duration-ms"] = int(duration * 1000)
logger.info("duration:%s url:%s parameters:%s", duration, request.path, request.GET.dict() )
# Code to be executed for each request/response after
# the view is called.
return response
def process_exception(self, request, exception):
if exception:
message = "url:{url} ** msg:{error} ````{tb}````".format(
url = request.build_absolute_uri(),
error = repr(exception),
tb = traceback.format_exc()
)
logger.warning(message)
# send dingtalk message
dingtalk.send(message)
# capture exception to sentry:
capture_exception(exception)
return HttpResponse("Error processing the request, please contact the system administrator.", status=500)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/tzone/django.git
git@gitee.com:tzone/django.git
tzone
django
Django
master

搜索帮助