From 10499a6864dfe5b71507ea39bf726d719a5adc55 Mon Sep 17 00:00:00 2001 From: Muhammed Zafar Date: Sun, 22 Oct 2023 14:23:31 +0530 Subject: [PATCH] [PATCH] Log non-JSON data (#1388) --- mulearnbackend/middlewares.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mulearnbackend/middlewares.py b/mulearnbackend/middlewares.py index 36d64a8a..987fb866 100644 --- a/mulearnbackend/middlewares.py +++ b/mulearnbackend/middlewares.py @@ -2,6 +2,7 @@ import json import logging import traceback +from contextlib import suppress import decouple from django.conf import settings @@ -111,10 +112,15 @@ def log_exception(self, request, exception): if settings.DEBUG: print(error_message) + data = request.body.decode('utf-8')) if hasattr(request, 'body') else 'No Data' + + with suppress(json.JSONDecodeError): + data = json.loads(data) + request_info = ( f"Request Info: METHOD: {request.method}; \n" f"\tPATH: {request.path}; \n" - f"\tDATA: {json.loads(request.body.decode('utf-8')) if hasattr(request, 'body') else 'No Data'}\n" + f"\tDATA: {data}\n" ) logger.error(request_info)