diff --git a/fyle_qbo_api/logging_middleware.py b/fyle_qbo_api/logging_middleware.py index 2456cb5c..8da1a3fd 100644 --- a/fyle_qbo_api/logging_middleware.py +++ b/fyle_qbo_api/logging_middleware.py @@ -1,3 +1,4 @@ +import json import logging import traceback import random @@ -52,3 +53,22 @@ def filter(self, record): worker_id = getattr(record, 'worker_id', '') record.worker_id = worker_id return True + + +class LogPostRequestMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + if request.method in ['POST', 'PUT'] : + try: + body_unicode = request.body.decode('utf-8') + request_body = json.loads(body_unicode) + logger.info("POST request to %s: %s", request.path, request_body) + except (json.JSONDecodeError, UnicodeDecodeError): + logger.warning("Failed to decode POST request body for %s", request.path) + except Exception as e: + logger.info('Something went wrong when logging post call - %s', e) + + response = self.get_response(request) + return response diff --git a/fyle_qbo_api/settings.py b/fyle_qbo_api/settings.py index 96a9bffb..aeb181c6 100644 --- a/fyle_qbo_api/settings.py +++ b/fyle_qbo_api/settings.py @@ -62,6 +62,7 @@ MIDDLEWARE = [ 'request_logging.middleware.LoggingMiddleware', 'fyle_qbo_api.logging_middleware.ErrorHandlerMiddleware', + 'fyle_qbo_api.logging_middleware.LogPostRequestMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', 'corsheaders.middleware.CorsPostCsrfMiddleware',