Skip to content

Commit 99dd974

Browse files
✨ Log only selected response status codes.
1 parent bc1cc8c commit 99dd974

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

drf_api_logger/middleware/api_logger_middleware.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ def __init__(self, get_response):
5151
settings.DRF_API_LOGGER_METHODS) is list:
5252
self.DRF_API_LOGGER_METHODS = settings.DRF_API_LOGGER_METHODS
5353

54+
self.DRF_API_LOGGER_STATUS_CODES = []
55+
if hasattr(settings, 'DRF_API_LOGGER_STATUS_CODES'):
56+
if type(settings.DRF_API_LOGGER_STATUS_CODES) is tuple or type(
57+
settings.DRF_API_LOGGER_STATUS_CODES) is list:
58+
self.DRF_API_LOGGER_STATUS_CODES = settings.DRF_API_LOGGER_STATUS_CODES
59+
5460
def __call__(self, request):
5561

5662
# Run only if logger is enabled.
@@ -82,6 +88,10 @@ def __call__(self, request):
8288
# the view (and later middleware) are called.
8389
response = self.get_response(request)
8490

91+
# Only log required status codes if matching
92+
if self.DRF_API_LOGGER_STATUS_CODES and response.status_code not in self.DRF_API_LOGGER_STATUS_CODES:
93+
return response
94+
8595
# Code to be executed for each request/response after
8696
# the view is called.
8797

0 commit comments

Comments
 (0)