Skip to content

Commit 8602734

Browse files
Merge pull request #32 from vishalanandl177/staging
API field length to 1024, masking available for list of dicts, timezone added
2 parents 7efec87 + c2ca027 commit 8602734

File tree

7 files changed

+61
-22
lines changed

7 files changed

+61
-22
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# DRF API Logger
2-
![version](https://img.shields.io/badge/version-1.0.8-blue.svg)
2+
![version](https://img.shields.io/badge/version-1.0.9-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)
@@ -175,13 +175,6 @@ Make sure to migrate the database specified in DRF_API_LOGGER_DEFAULT_DATABASE.
175175
"""
176176
```
177177

178-
### API with or without Host
179-
You can specify an endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
180-
```python
181-
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
182-
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
183-
```
184-
185178
### Want to identify slow APIs? (Optional)
186179
You can also identify slow APIs by specifying `DRF_API_LOGGER_SLOW_API_ABOVE` in settings.py.
187180

@@ -191,6 +184,26 @@ DRF_API_LOGGER_SLOW_API_ABOVE = 200 # Default to None
191184
# Specify in milli-seconds.
192185
```
193186

187+
### Want to see the API information in local timezone? (Optional)
188+
You can also identify slow APIs by specifying `DRF_API_LOGGER_TIMEDELTA` in settings.py.
189+
It is going to display the API request time after adding the timedelta specified in the settings.py file.
190+
It won't change the Database timezone.
191+
```python
192+
DRF_API_LOGGER_TIMEDELTA = 330 # UTC + 330 Minutes = IST (5:Hours, 30:Minutes ahead from UTC)
193+
# Specify in minutes.
194+
```
195+
```python
196+
# Yoc can specify negative values for the countries behind the UTC timezone.
197+
DRF_API_LOGGER_TIMEDELTA = -30 # Example
198+
```
199+
200+
### API with or without Host
201+
You can specify an endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
202+
```python
203+
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
204+
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
205+
```
206+
194207
Considering we are accessing the following URL: http://127.0.0.1:8000/api/v1/?page=123
195208
DRF_API_LOGGER_PATH_TYPE possible values are:
196209
1. ABSOLUTE (Default) :
@@ -232,7 +245,7 @@ DRF API Logger Model:
232245
```
233246
class APILogsModel(Model):
234247
id = models.BigAutoField(primary_key=True)
235-
api = models.CharField(max_length=512, help_text='API URL')
248+
api = models.CharField(max_length=1024, help_text='API URL')
236249
headers = models.TextField()
237250
body = models.TextField()
238251
method = models.CharField(max_length=10, db_index=True)

dist/drf_api_logger-1.0.9.tar.gz

16.4 KB
Binary file not shown.

drf_api_logger/admin.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import timedelta
2+
13
from django.conf import settings
24
from django.contrib import admin
35
from django.db.models import Count
@@ -17,7 +19,7 @@ def __init__(self, request, params, model, model_admin):
1719
super().__init__(request, params, model, model_admin)
1820
if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'):
1921
if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value.
20-
self.DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.
22+
self._DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.
2123

2224
def lookups(self, request, model_admin):
2325
"""
@@ -46,24 +48,26 @@ def queryset(self, request, queryset):
4648
"""
4749
# to decide how to filter the queryset.
4850
if self.value() == 'slow':
49-
return queryset.filter(execution_time__gte=self.DRF_API_LOGGER_SLOW_API_ABOVE)
51+
return queryset.filter(execution_time__gte=self._DRF_API_LOGGER_SLOW_API_ABOVE)
5052
if self.value() == 'fast':
51-
return queryset.filter(execution_time__lt=self.DRF_API_LOGGER_SLOW_API_ABOVE)
53+
return queryset.filter(execution_time__lt=self._DRF_API_LOGGER_SLOW_API_ABOVE)
5254

5355
return queryset
5456

5557
class APILogsAdmin(admin.ModelAdmin):
5658

5759
def __init__(self, model, admin_site):
5860
super().__init__(model, admin_site)
59-
self.DRF_API_LOGGER_SLOW_API_ABOVE = None
61+
self._DRF_API_LOGGER_TIMEDELTA = 0
6062
if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'):
6163
if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value.
62-
self.DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.
6364
self.list_filter += (SlowAPIsFilter,)
65+
if hasattr(settings, 'DRF_API_LOGGER_TIMEDELTA'):
66+
if type(settings.DRF_API_LOGGER_TIMEDELTA) == int: # Making sure for integer value.
67+
self._DRF_API_LOGGER_TIMEDELTA = settings.DRF_API_LOGGER_TIMEDELTA
6468

6569
def added_on_time(self, obj):
66-
return obj.added_on.strftime("%d %b %Y %H:%M:%S")
70+
return (obj.added_on + timedelta(minutes=self._DRF_API_LOGGER_TIMEDELTA)).strftime("%d %b %Y %H:%M:%S")
6771

6872
added_on_time.admin_order_field = 'added_on'
6973
added_on_time.short_description = 'Added on'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.1.5 on 2021-12-21 16:25
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('drf_api_logger', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='apilogsmodel',
15+
name='api',
16+
field=models.CharField(help_text='API URL', max_length=1024),
17+
),
18+
]

drf_api_logger/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Meta:
2121

2222

2323
class APILogsModel(BaseModel):
24-
api = models.CharField(max_length=512, help_text='API URL')
24+
api = models.CharField(max_length=1024, help_text='API URL')
2525
headers = models.TextField()
2626
body = models.TextField()
2727
method = models.CharField(max_length=10, db_index=True)

drf_api_logger/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
if type(settings.DRF_API_LOGGER_EXCLUDE_KEYS) in (list, tuple):
77
SENSITIVE_KEYS.extend(settings.DRF_API_LOGGER_EXCLUDE_KEYS)
88

9+
910
def get_headers(request=None):
1011
"""
1112
Function: get_headers(self, request)
@@ -53,13 +54,16 @@ def mask_sensitive_data(data):
5354
"""
5455

5556
if type(data) != dict:
56-
return data
57+
return data
5758

5859
for key, value in data.items():
59-
if key in SENSITIVE_KEYS:
60-
data[key] = "***FILTERED***"
60+
if key in SENSITIVE_KEYS:
61+
data[key] = "***FILTERED***"
62+
63+
if type(value) == dict:
64+
data[key] = mask_sensitive_data(data[key])
6165

62-
if type(value) == dict:
63-
data[key] = mask_sensitive_data(data[key])
66+
if type(value) == list:
67+
data[key] = [mask_sensitive_data(item) for item in data[key]]
6468

6569
return data

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.8",
24+
version="1.0.9",
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)