Skip to content

Commit f515f54

Browse files
Merge pull request #37 from vishalanandl177/staging
1. [DjangoAdmin] KeyError when trying to delete multiple API Logger entry issue fixed. 2. New feature added to log only selected methods.
2 parents 8602734 + 89b1fce commit f515f54

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# DRF API Logger
2-
![version](https://img.shields.io/badge/version-1.0.9-blue.svg)
2+
![version](https://img.shields.io/badge/version-1.0.10-blue.svg)
33
[![Downloads](https://static.pepy.tech/personalized-badge/drf-api-logger?period=total&units=none&left_color=black&right_color=orange&left_text=Downloads%20Total)](http://pepy.tech/project/drf-api-logger)
44
[![Downloads](https://static.pepy.tech/personalized-badge/drf-api-logger?period=month&units=none&left_color=black&right_color=orange&left_text=Downloads%20Last%20Month)](https://pepy.tech/project/drf-api-logger)
55
[![Downloads](https://static.pepy.tech/personalized-badge/drf-api-logger?period=week&units=none&left_color=black&right_color=orange&left_text=Downloads%20Last%20Week)](https://pepy.tech/project/drf-api-logger)
66
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
77
[![Donate](https://img.shields.io/badge/$-support-ff69b4.svg?style=flat)](https://paypal.me/chynybekov)
88

9-
<a href="https://discord.gg/Kr8SbgPe"><img src="https://img.shields.io/badge/Discord-7289DA?style=for-the-badge&logo=discord&logoColor=white" alt="Join Community Badge"/></a>
9+
<a href="https://discord.gg/eeYansFDCT"><img src="https://img.shields.io/badge/Discord-7289DA?style=for-the-badge&logo=discord&logoColor=white" alt="Join Community Badge"/></a>
1010
<a href="https://www.instagram.com/coderssecret/"><img src="https://img.shields.io/badge/Instagram-E4405F?style=for-the-badge&logo=instagram&logoColor=white" alt="Join Instagram"/></a>
1111
<a href="https://github.com/vishalanandl177"><img src="https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white" alt="GitHub"/></a>
1212

@@ -184,6 +184,12 @@ DRF_API_LOGGER_SLOW_API_ABOVE = 200 # Default to None
184184
# Specify in milli-seconds.
185185
```
186186

187+
### Want to log only selected request methods? (Optional)
188+
You can log only selected methods by specifying `DRF_API_LOGGER_METHODS` in settings.py.
189+
```python
190+
DRF_API_LOGGER_METHODS = ['GET', 'POST', 'DELETE', 'PUT'] # Default to empty list (Log all the requests).
191+
```
192+
187193
### Want to see the API information in local timezone? (Optional)
188194
You can also identify slow APIs by specifying `DRF_API_LOGGER_TIMEDELTA` in settings.py.
189195
It is going to display the API request time after adding the timedelta specified in the settings.py file.

drf_api_logger/admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def added_on_time(self, obj):
8787

8888
def changelist_view(self, request, extra_context=None):
8989
response = super(APILogsAdmin, self).changelist_view(request, extra_context)
90-
filtered_query_set = response.context_data["cl"].queryset
90+
try:
91+
filtered_query_set = response.context_data["cl"].queryset
92+
except:
93+
return response
9194
analytics_model = filtered_query_set.values('added_on__date').annotate(total=Count('id')).order_by('total')
9295
status_code_count_mode = filtered_query_set.values('id').values('status_code').annotate(
9396
total=Count('id')).order_by('status_code')

drf_api_logger/middleware/api_logger_middleware.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def __init__(self, get_response):
4545
settings.DRF_API_LOGGER_SKIP_NAMESPACE) is list:
4646
self.DRF_API_LOGGER_SKIP_NAMESPACE = settings.DRF_API_LOGGER_SKIP_NAMESPACE
4747

48+
self.DRF_API_LOGGER_METHODS = []
49+
if hasattr(settings, 'DRF_API_LOGGER_METHODS'):
50+
if type(settings.DRF_API_LOGGER_METHODS) is tuple or type(
51+
settings.DRF_API_LOGGER_METHODS) is list:
52+
self.DRF_API_LOGGER_METHODS = settings.DRF_API_LOGGER_METHODS
53+
4854
def __call__(self, request):
4955

5056
# Run only if logger is enabled.
@@ -81,6 +87,11 @@ def __call__(self, request):
8187

8288
headers = get_headers(request=request)
8389
method = request.method
90+
91+
# Log only registered methods if available.
92+
if len(self.DRF_API_LOGGER_METHODS) > 0 and method not in self.DRF_API_LOGGER_METHODS:
93+
return self.get_response(request)
94+
8495
if response.get('content-type') in ('application/json', 'application/vnd.api+json',):
8596
if getattr(response, 'streaming', False):
8697
response_body = '** Streaming **'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_long_desc():
2121

2222
setuptools.setup(
2323
name="drf_api_logger",
24-
version="1.0.9",
24+
version="1.0.10",
2525
author="Vishal Anand",
2626
author_email="[email protected]",
2727
description="An API Logger for your Django Rest Framework project.",

0 commit comments

Comments
 (0)