Skip to content

Commit aa2d940

Browse files
Merge pull request #97 from abe-101/patch-1
Fix middleware handling to skip logging for static and media file requests
2 parents 0ec54c8 + f8eb112 commit aa2d940

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

drf_api_logger/middleware/api_logger_middleware.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,16 @@ def __init__(self, get_response):
8484
if type(settings.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE) is int:
8585
self.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE = settings.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE
8686

87+
def is_static_or_media_request(self, path):
88+
static_url = getattr(settings, 'STATIC_URL', '/static/')
89+
media_url = getattr(settings, 'MEDIA_URL', '/media/')
90+
91+
return path.startswith(static_url) or path.startswith(media_url)
92+
8793
def __call__(self, request):
94+
# Skip logging for static and media files
95+
if self.is_static_or_media_request(request.path):
96+
return self.get_response(request)
8897

8998
# Run only if logger is enabled.
9099
if self.DRF_API_LOGGER_DATABASE or self.DRF_API_LOGGER_SIGNAL:
@@ -155,6 +164,7 @@ def __call__(self, request):
155164
"application/vnd.api+json",
156165
"application/gzip",
157166
"application/octet-stream",
167+
"text/calendar",
158168
]
159169
if hasattr(settings, "DRF_API_LOGGER_CONTENT_TYPES") and type(
160170
settings.DRF_API_LOGGER_CONTENT_TYPES
@@ -170,6 +180,9 @@ def __call__(self, request):
170180
response_body = '** Binary File **'
171181
elif getattr(response, 'streaming', False):
172182
response_body = '** Streaming **'
183+
elif response.get('content-type') == 'text/calendar':
184+
response_body = '** Calendar **'
185+
173186
else:
174187
if type(response.content) is bytes:
175188
response_body = json.loads(response.content.decode())

0 commit comments

Comments
 (0)