Skip to content

Commit

Permalink
Merge pull request #32 from vishalanandl177/staging
Browse files Browse the repository at this point in the history
API field length to 1024, masking available for list of dicts, timezone added
  • Loading branch information
vishalanandl177 committed Dec 21, 2021
2 parents 7efec87 + c2ca027 commit 8602734
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
31 changes: 22 additions & 9 deletions README.md
@@ -1,5 +1,5 @@
# DRF API Logger
![version](https://img.shields.io/badge/version-1.0.8-blue.svg)
![version](https://img.shields.io/badge/version-1.0.9-blue.svg)
[![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)
[![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)
[![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)
Expand Down Expand Up @@ -175,13 +175,6 @@ Make sure to migrate the database specified in DRF_API_LOGGER_DEFAULT_DATABASE.
"""
```

### API with or without Host
You can specify an endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
```python
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
```

### Want to identify slow APIs? (Optional)
You can also identify slow APIs by specifying `DRF_API_LOGGER_SLOW_API_ABOVE` in settings.py.

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

### Want to see the API information in local timezone? (Optional)
You can also identify slow APIs by specifying `DRF_API_LOGGER_TIMEDELTA` in settings.py.
It is going to display the API request time after adding the timedelta specified in the settings.py file.
It won't change the Database timezone.
```python
DRF_API_LOGGER_TIMEDELTA = 330 # UTC + 330 Minutes = IST (5:Hours, 30:Minutes ahead from UTC)
# Specify in minutes.
```
```python
# Yoc can specify negative values for the countries behind the UTC timezone.
DRF_API_LOGGER_TIMEDELTA = -30 # Example
```

### API with or without Host
You can specify an endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
```python
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
```

Considering we are accessing the following URL: http://127.0.0.1:8000/api/v1/?page=123
DRF_API_LOGGER_PATH_TYPE possible values are:
1. ABSOLUTE (Default) :
Expand Down Expand Up @@ -232,7 +245,7 @@ DRF API Logger Model:
```
class APILogsModel(Model):
id = models.BigAutoField(primary_key=True)
api = models.CharField(max_length=512, help_text='API URL')
api = models.CharField(max_length=1024, help_text='API URL')
headers = models.TextField()
body = models.TextField()
method = models.CharField(max_length=10, db_index=True)
Expand Down
Binary file added dist/drf_api_logger-1.0.9.tar.gz
Binary file not shown.
16 changes: 10 additions & 6 deletions drf_api_logger/admin.py
@@ -1,3 +1,5 @@
from datetime import timedelta

from django.conf import settings
from django.contrib import admin
from django.db.models import Count
Expand All @@ -17,7 +19,7 @@ def __init__(self, request, params, model, model_admin):
super().__init__(request, params, model, model_admin)
if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'):
if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value.
self.DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.
self._DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.

def lookups(self, request, model_admin):
"""
Expand Down Expand Up @@ -46,24 +48,26 @@ def queryset(self, request, queryset):
"""
# to decide how to filter the queryset.
if self.value() == 'slow':
return queryset.filter(execution_time__gte=self.DRF_API_LOGGER_SLOW_API_ABOVE)
return queryset.filter(execution_time__gte=self._DRF_API_LOGGER_SLOW_API_ABOVE)
if self.value() == 'fast':
return queryset.filter(execution_time__lt=self.DRF_API_LOGGER_SLOW_API_ABOVE)
return queryset.filter(execution_time__lt=self._DRF_API_LOGGER_SLOW_API_ABOVE)

return queryset

class APILogsAdmin(admin.ModelAdmin):

def __init__(self, model, admin_site):
super().__init__(model, admin_site)
self.DRF_API_LOGGER_SLOW_API_ABOVE = None
self._DRF_API_LOGGER_TIMEDELTA = 0
if hasattr(settings, 'DRF_API_LOGGER_SLOW_API_ABOVE'):
if type(settings.DRF_API_LOGGER_SLOW_API_ABOVE) == int: # Making sure for integer value.
self.DRF_API_LOGGER_SLOW_API_ABOVE = settings.DRF_API_LOGGER_SLOW_API_ABOVE / 1000 # Converting to seconds.
self.list_filter += (SlowAPIsFilter,)
if hasattr(settings, 'DRF_API_LOGGER_TIMEDELTA'):
if type(settings.DRF_API_LOGGER_TIMEDELTA) == int: # Making sure for integer value.
self._DRF_API_LOGGER_TIMEDELTA = settings.DRF_API_LOGGER_TIMEDELTA

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

added_on_time.admin_order_field = 'added_on'
added_on_time.short_description = 'Added on'
Expand Down
18 changes: 18 additions & 0 deletions drf_api_logger/migrations/0002_auto_20211221_2155.py
@@ -0,0 +1,18 @@
# Generated by Django 3.1.5 on 2021-12-21 16:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('drf_api_logger', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='apilogsmodel',
name='api',
field=models.CharField(help_text='API URL', max_length=1024),
),
]
2 changes: 1 addition & 1 deletion drf_api_logger/models.py
Expand Up @@ -21,7 +21,7 @@ class Meta:


class APILogsModel(BaseModel):
api = models.CharField(max_length=512, help_text='API URL')
api = models.CharField(max_length=1024, help_text='API URL')
headers = models.TextField()
body = models.TextField()
method = models.CharField(max_length=10, db_index=True)
Expand Down
14 changes: 9 additions & 5 deletions drf_api_logger/utils.py
Expand Up @@ -6,6 +6,7 @@
if type(settings.DRF_API_LOGGER_EXCLUDE_KEYS) in (list, tuple):
SENSITIVE_KEYS.extend(settings.DRF_API_LOGGER_EXCLUDE_KEYS)


def get_headers(request=None):
"""
Function: get_headers(self, request)
Expand Down Expand Up @@ -53,13 +54,16 @@ def mask_sensitive_data(data):
"""

if type(data) != dict:
return data
return data

for key, value in data.items():
if key in SENSITIVE_KEYS:
data[key] = "***FILTERED***"
if key in SENSITIVE_KEYS:
data[key] = "***FILTERED***"

if type(value) == dict:
data[key] = mask_sensitive_data(data[key])

if type(value) == dict:
data[key] = mask_sensitive_data(data[key])
if type(value) == list:
data[key] = [mask_sensitive_data(item) for item in data[key]]

return data
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -21,7 +21,7 @@ def get_long_desc():

setuptools.setup(
name="drf_api_logger",
version="1.0.8",
version="1.0.9",
author="Vishal Anand",
author_email="vishalanandl177@gmail.com",
description="An API Logger for your Django Rest Framework project.",
Expand Down

0 comments on commit 8602734

Please sign in to comment.