diff --git a/README.md b/README.md index bda54a0..bf5a30f 100644 --- a/README.md +++ b/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) @@ -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. @@ -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) : @@ -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) diff --git a/dist/drf_api_logger-1.0.9.tar.gz b/dist/drf_api_logger-1.0.9.tar.gz new file mode 100644 index 0000000..20b5bac Binary files /dev/null and b/dist/drf_api_logger-1.0.9.tar.gz differ diff --git a/drf_api_logger/admin.py b/drf_api_logger/admin.py index bcc5713..304629c 100644 --- a/drf_api_logger/admin.py +++ b/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 @@ -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): """ @@ -46,9 +48,9 @@ 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 @@ -56,14 +58,16 @@ 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' diff --git a/drf_api_logger/migrations/0002_auto_20211221_2155.py b/drf_api_logger/migrations/0002_auto_20211221_2155.py new file mode 100644 index 0000000..be31ffe --- /dev/null +++ b/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), + ), + ] diff --git a/drf_api_logger/models.py b/drf_api_logger/models.py index be31d5f..72f312a 100644 --- a/drf_api_logger/models.py +++ b/drf_api_logger/models.py @@ -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) diff --git a/drf_api_logger/utils.py b/drf_api_logger/utils.py index a19f625..1a5edf5 100644 --- a/drf_api_logger/utils.py +++ b/drf_api_logger/utils.py @@ -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) @@ -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 diff --git a/setup.py b/setup.py index 9f8c774..724450f 100644 --- a/setup.py +++ b/setup.py @@ -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.",