From 84d79fa79ab72353fcb3524529e1be7f560b7a93 Mon Sep 17 00:00:00 2001 From: Abe Hanoka Date: Fri, 7 Jun 2024 15:55:44 -0400 Subject: [PATCH 1/2] Fix middleware handling to skip logging for static and media file requests fixes #91 --- drf_api_logger/middleware/api_logger_middleware.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drf_api_logger/middleware/api_logger_middleware.py b/drf_api_logger/middleware/api_logger_middleware.py index 8134608..5b0b2cd 100644 --- a/drf_api_logger/middleware/api_logger_middleware.py +++ b/drf_api_logger/middleware/api_logger_middleware.py @@ -84,7 +84,16 @@ def __init__(self, get_response): if type(settings.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE) is int: self.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE = settings.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE + def is_static_or_media_request(self, path): + static_url = getattr(settings, 'STATIC_URL', '/static/') + media_url = getattr(settings, 'MEDIA_URL', '/media/') + + return path.startswith(static_url) or path.startswith(media_url) + def __call__(self, request): + # Skip logging for static and media files + if self.is_static_or_media_request(request.path): + return self.get_response(request) # Run only if logger is enabled. if self.DRF_API_LOGGER_DATABASE or self.DRF_API_LOGGER_SIGNAL: From f8eb1120ed0616db4ac387572d5c77e0685a53d3 Mon Sep 17 00:00:00 2001 From: Abe Hanoka Date: Fri, 7 Jun 2024 18:26:46 -0400 Subject: [PATCH 2/2] add text/calendar support --- drf_api_logger/middleware/api_logger_middleware.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drf_api_logger/middleware/api_logger_middleware.py b/drf_api_logger/middleware/api_logger_middleware.py index 5b0b2cd..f73eaf6 100644 --- a/drf_api_logger/middleware/api_logger_middleware.py +++ b/drf_api_logger/middleware/api_logger_middleware.py @@ -164,6 +164,7 @@ def __call__(self, request): "application/vnd.api+json", "application/gzip", "application/octet-stream", + "text/calendar", ] if hasattr(settings, "DRF_API_LOGGER_CONTENT_TYPES") and type( settings.DRF_API_LOGGER_CONTENT_TYPES @@ -179,6 +180,9 @@ def __call__(self, request): response_body = '** Binary File **' elif getattr(response, 'streaming', False): response_body = '** Streaming **' + elif response.get('content-type') == 'text/calendar': + response_body = '** Calendar **' + else: if type(response.content) is bytes: response_body = json.loads(response.content.decode())