Skip to content

Commit

Permalink
[PATCH] Log non-JSON data (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
MZaFaRM authored Oct 22, 2023
1 parent f6c8724 commit 10499a6
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion mulearnbackend/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import traceback
from contextlib import suppress

import decouple
from django.conf import settings
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 10499a6

Please sign in to comment.