requestRecord.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time : 2021/9/22 17:08
  4. @Auth : Locky
  5. @File :requestRecord.py
  6. @IDE :PyCharm
  7. """
  8. import json
  9. import logging
  10. import time
  11. from django.utils.deprecation import MiddlewareMixin
  12. from Model.models import RequestRecordModel
  13. class RequestRecordMiddleware(MiddlewareMixin):
  14. def process_request(self, request):
  15. request.start_time = time.time()
  16. def process_response(self, request, response):
  17. try:
  18. execute_time = time.time() - request.start_time
  19. method = request.method
  20. url = request.path
  21. # 获取请求参数并转为字符串
  22. if method == 'GET':
  23. parameter = json.dumps(request.GET.dict())
  24. elif method == 'POST':
  25. parameter = json.dumps(request.POST.dict())
  26. else:
  27. parameter = ''
  28. if response.status_code == 500: # 处理没有捕获异常的情况
  29. request_record_data = {
  30. 'method': method,
  31. 'url': url,
  32. 'parameter': parameter,
  33. 'execute_time': execute_time,
  34. 'status_code': 500,
  35. 'reason_phrase': response.reason_phrase,
  36. }
  37. RequestRecordModel.objects.create(**request_record_data)
  38. elif response.content: # 处理捕获异常的情况
  39. # print('content: ', response.content)
  40. content = eval(str(response.content, 'utf-8')) # bytes 转为 dict
  41. # logger = logging.getLogger('info')
  42. # logger.info('content: {}'.format(content))
  43. error_flag = False
  44. reason_phrase = ''
  45. if 'result_code' in content and (content['result_code'] == 500 or content['result_code'] == 474):
  46. reason_phrase = content['result']
  47. error_flag = True
  48. elif 'code' in content and content['code'] == 500: # ResponseObject.returntype == 'pc'
  49. reason_phrase = content['data']
  50. error_flag = True
  51. if error_flag:
  52. request_record_data = {
  53. 'method': method,
  54. 'url': url,
  55. 'parameter': parameter,
  56. 'execute_time': execute_time,
  57. 'status_code': 500,
  58. 'reason_phrase': reason_phrase,
  59. }
  60. RequestRecordModel.objects.create(**request_record_data)
  61. finally:
  62. return response
  63. class LogMiddleware:
  64. def __init__(self, get_response):
  65. self.get_response = get_response
  66. def __call__(self, request):
  67. # 响应
  68. response = self.get_response(request)
  69. json_obj = json.loads(response.content)
  70. # 请求是否成功
  71. if json_obj['result_code'] != 0:
  72. LOGGER = logging.getLogger('error')
  73. # 记录请求
  74. path = request.path
  75. if request.method == 'POST':
  76. # GET请求逻辑处理
  77. parameter = request.POST
  78. LOGGER.error(
  79. '请求路径:{}, 请求方式:{}, 输入数据:{}, 输出数据:{}, 响应状态:{}'.format(path, request.method, parameter, json_obj,
  80. response.status_code))
  81. elif request.method == 'GET':
  82. parameter = request.GET
  83. LOGGER.error(
  84. '请求路径:{}, 请求方式:{}, 输入数据:{}, 输出数据:{}, 响应状态:{}'.format(path, request.method, parameter, json_obj,
  85. response.status_code))
  86. return response