Исправление двойного логирования ошибок

This commit is contained in:
2026-07-19 19:45:03 +03:00
parent 7c13582fdb
commit a6c9db1a71
6 changed files with 80 additions and 24 deletions

View File

@@ -63,7 +63,7 @@ class CustomLoggingMiddleware:
# Обрабатываем данные запроса, ответа и перехваченного исключения
body = self._process_body(request)
exc = getattr(request, "_raised_exception", None)
exc = getattr(response, "_raised_exception", None)
status_code = response.status_code
request_id = request.META.get('HTTP_X_REQUEST_ID', '')
response_content = self._process_response(response)
@@ -172,18 +172,19 @@ class CustomLoggingMiddleware:
:param exc: Исключение
"""
if exc:
log_message += f"\nException: {str(exc)}"
if exc or status_code >= status.HTTP_400_BAD_REQUEST:
if status_code == status.HTTP_400_BAD_REQUEST:
self.logger.debug(log_message, exc_info=exc)
elif status_code in [status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]: # UNAUTHORIZED, FORBIDDEN
elif status_code == status.HTTP_401_UNAUTHORIZED:
self.logger.warning(log_message, exc_info=exc)
elif status_code == status.HTTP_403_FORBIDDEN:
self.logger.warning(log_message, exc_info=exc)
elif status_code == status.HTTP_404_NOT_FOUND:
self.logger.error(log_message, exc_info=exc)
else:
self.logger.critical(log_message, exc_info=exc)
else:
self.logger.debug(log_message)