Skip to content

Commit

Permalink
feat: Add middleware to log all post requests (#568)
Browse files Browse the repository at this point in the history
* feat: Add middleware to log all post requests

* corrected code
  • Loading branch information
Ashutosh619-sudo authored Oct 11, 2024
1 parent 6c71f00 commit 609c36b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions fyle_intacct_api/logging_middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import traceback
import os
Expand Down Expand Up @@ -56,3 +57,21 @@ 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
1 change: 1 addition & 0 deletions fyle_intacct_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

MIDDLEWARE = [
'request_logging.middleware.LoggingMiddleware',
'fyle_intacct_api.logging_middleware.LogPostRequestMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsPostCsrfMiddleware',
Expand Down

0 comments on commit 609c36b

Please sign in to comment.