diff --git a/mulearnbackend/middlewares.py b/mulearnbackend/middlewares.py index 987fb866..5655b982 100644 --- a/mulearnbackend/middlewares.py +++ b/mulearnbackend/middlewares.py @@ -1,8 +1,9 @@ +from contextlib import suppress import hmac import json +import json import logging import traceback -from contextlib import suppress import decouple from django.conf import settings @@ -95,7 +96,7 @@ def __call__(self, request): def log_exception(self, request, exception): """ - Log the exception and prints the information in CLI if DEBUG is True. + Log the exception and prints the information in CLI. Args: request: The request object. @@ -109,24 +110,27 @@ def log_exception(self, request, exception): ) logger.error(error_message) - if settings.DEBUG: - print(error_message) + print(error_message) - data = request.body.decode('utf-8')) if hasattr(request, 'body') else 'No Data' + body = request.body.decode("utf-8") if hasattr(request, "body") else "No body" + auth = request.auth if hasattr(request, "auth") else "No Auth data" + + with suppress(json.JSONDecodeError): + body = json.loads(body) + body = json.dumps(body, indent=4) with suppress(json.JSONDecodeError): - data = json.loads(data) + auth = json.dumps(auth, indent=4) request_info = ( f"Request Info: METHOD: {request.method}; \n" - f"\tPATH: {request.path}; \n" - f"\tDATA: {data}\n" + f"PATH: {request.path}; \n" + f"AUTH: \n{auth} \n" + f"BODY: \n{body}\n" ) logger.error(request_info) - # Print to terminal if DEBUG is True - if settings.DEBUG: - print(request_info) + print(request_info) def process_exception(self, request, exception): """ diff --git a/mulearnbackend/settings.py b/mulearnbackend/settings.py index bcf502a0..daa1faca 100644 --- a/mulearnbackend/settings.py +++ b/mulearnbackend/settings.py @@ -57,7 +57,7 @@ "debug_toolbar.middleware.DebugToolbarMiddleware", "django.middleware.common.CommonMiddleware", "corsheaders.middleware.CorsMiddleware", - # "mulearnbackend.middlewares.UniversalErrorHandlerMiddleware", + "mulearnbackend.middlewares.UniversalErrorHandlerMiddleware", ] ROOT_URLCONF = "mulearnbackend.urls"