Skip to content

Commit

Permalink
feat: Log post requests and sdk changes (#400)
Browse files Browse the repository at this point in the history
* feat: Log post requests and sdk changes

* pylint resolved
  • Loading branch information
Ashutosh619-sudo committed Oct 15, 2024
1 parent bf4f570 commit 38a46c4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
20 changes: 20 additions & 0 deletions fyle_xero_api/logging_middleware.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json
import logging
import traceback

from django.conf import settings
from django.http import HttpResponse

logger = logging.getLogger(__name__)
logger.level = logging.INFO


class ErrorHandlerMiddleware:
Expand All @@ -25,3 +27,21 @@ def process_exception(self, request, exception):
logger.error(str(message).replace("\n", ""))

return HttpResponse("Error processing the request.", status=500)


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
16 changes: 15 additions & 1 deletion fyle_xero_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
]

MIDDLEWARE = [
"fyle_xero_api.logging_middleware.LogPostRequestMiddleware",
"request_logging.middleware.LoggingMiddleware",
"fyle_xero_api.logging_middleware.ErrorHandlerMiddleware",
"corsheaders.middleware.CorsMiddleware",
Expand Down Expand Up @@ -148,7 +149,7 @@

LOGGING = {
"version": 1,
"disable_existing_loggers": True,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "{levelname} %s {asctime} {module} {message} " % SERVICE_NAME,
Expand All @@ -158,6 +159,10 @@
"format": "request {levelname} %s {asctime} {message}" % SERVICE_NAME,
"style": "{",
},
"standard": {
"format": "{levelname} %s {asctime} {name} {message}" % SERVICE_NAME,
"style": "{",
},
},
"handlers": {
"debug_logs": {
Expand All @@ -170,6 +175,10 @@
"stream": sys.stdout,
"formatter": "requests",
},
"console": {
"class": "logging.StreamHandler",
"formatter": "standard",
},
},
"loggers": {
"django": {
Expand Down Expand Up @@ -200,6 +209,11 @@
"handlers": ["debug_logs"],
"propagate": True
},
"xerosdk.apis.api_base": {
"handlers": ["console"],
"level": "INFO",
"propagate": True
},
},
}

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ djangorestframework==3.11.2
django-sendgrid-v5==1.2.0
enum34==1.1.10
future==0.18.2
fyle==0.37.0
fyle==0.37.2
fyle-accounting-mappings==1.34.8
fyle-integrations-platform-connector==1.38.4
fyle-rest-auth==1.7.2
Expand All @@ -41,7 +41,7 @@ Unidecode==1.1.2
urllib3==1.26.11
wcwidth==0.1.8
wrapt==1.12.1
xerosdk==0.14.1
xerosdk==0.14.2
pytest==7.1.2
pytest-cov==3.0.0
pytest-django==4.5.2
Expand Down

0 comments on commit 38a46c4

Please sign in to comment.