Skip to content

Commit a6e1b34

Browse files
Merge pull request #49 from Hassan-Elseoudy/feature/status_codes
✨ Log only selected response status code.
2 parents bc1cc8c + d61b789 commit a6e1b34

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ You can log only selected methods by specifying `DRF_API_LOGGER_METHODS` in sett
190190
DRF_API_LOGGER_METHODS = ['GET', 'POST', 'DELETE', 'PUT'] # Default to empty list (Log all the requests).
191191
```
192192

193+
### Want to log only selected response status codes? (Optional)
194+
You can log only selected responses by specifying `DRF_API_LOGGER_STATUS_CODES` in settings.py.
195+
```python
196+
DRF_API_LOGGER_STATUS_CODES = ['200', '400', '404', '500'] # Default to empty list (Log all responses).
197+
```
198+
193199
### Want to see the API information in local timezone? (Optional)
194200
You can also change the timezone by specifying `DRF_API_LOGGER_TIMEDELTA` in settings.py.
195201
It won't change the Database timezone. It will still remain UTC or the timezone you have defined.

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)