Skip to content

Commit

Permalink
Improvements and model usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalanandl177 committed Feb 22, 2021
1 parent d186aaf commit ef544cb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DRF API Logger
![version](https://img.shields.io/badge/version-1.0.2-blue.svg)
![version](https://img.shields.io/badge/version-1.0.3-blue.svg)
[![Downloads](https://pepy.tech/badge/drf-api-logger)](http://pepy.tech/project/drf-api-logger)
[![Downloads](https://pepy.tech/badge/drf-api-logger/month)](https://pepy.tech/project/drf-api-logger)
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
Expand All @@ -23,9 +23,9 @@ It logs all the API information for content type "application/json".
9. Client IP Address


You can log API information into the database or listen to the logger signals for different use-cases or you can do both.
You can log API information into the database or listen to the logger signals for different use-cases, or you can do both.

* The logger usage a separate thread to run so it won't affect your API response time.
* The logger usage a separate thread to run, so it won't affect your API response time.

## Installation

Expand Down Expand Up @@ -154,7 +154,7 @@ DRF_API_LOGGER_SKIP_URL_NAME = ['url_name1', 'url_name2']
Note: It does not log Django Admin Panel API calls.

### API with or without Host
You can specify endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
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
Expand All @@ -179,4 +179,36 @@ DRF_API_LOGGER_PATH_TYPE possible values are:

Output: ```http://127.0.0.1:8000/api/v1/?page=123```

Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return insecure URI.
Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return an insecure URI.


### Use DRF API Logger Model to query
You can use the DRF API Logger Model to query some information.

Note: Make sure to set "DRF_API_LOGGER_DATABASE = True" in settings.py file.
```
from drf_api_logger.models import APILogsModel
"""
Example:
Select records for status_code 200.
"""
result_for_200_status_code = APILogsModel.objects.filter(status_code=200)
```

Model:
```
class APILogsModel(Model):
id = models.BigAutoField(primary_key=True)
api = models.CharField(max_length=512, help_text='API URL')
headers = models.TextField()
body = models.TextField()
method = models.CharField(max_length=10, db_index=True)
client_ip_address = models.CharField(max_length=50)
response = models.TextField()
status_code = models.PositiveSmallIntegerField(help_text='Response status code', db_index=True)
execution_time = models.DecimalField(decimal_places=5, max_digits=8,
help_text='Server execution time (Not complete response time.)')
added_on = models.DateTimeField()
```
7 changes: 4 additions & 3 deletions drf_api_logger/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.conf.urls import url
from django.contrib import admin
from django.db.models import Count
from django.shortcuts import render
from django.views import View


from drf_api_logger.utils import database_log_enabled

Expand Down Expand Up @@ -46,4 +44,7 @@ def has_add_permission(self, request, obj=None):
def has_change_permission(self, request, obj=None):
return False

def has_delete_permission(self, request, obj=None):
return False

admin.site.register(APILogsModel, APILogsAdmin)
2 changes: 2 additions & 0 deletions drf_api_logger/insert_log_into_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ def _insert_into_data_base(self, bulk_item):
Model does not exists.
Did you forget to migrate?
""")
except Exception as e:
print('DRF API LOGGER EXCEPTION:', e)
1 change: 1 addition & 0 deletions drf_api_logger/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from drf_api_logger.utils import database_log_enabled


if database_log_enabled():
"""
Load models only if DRF_API_LOGGER_DATABASE is True
Expand Down
3 changes: 2 additions & 1 deletion drf_api_logger/templates/charts_change_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{
label: 'Number of API calls',
data: chartData,
backgroundColor: 'rgba(220,20,20,0.5)',
backgroundColor: 'rgb(33,150,243)',
},
],
},
Expand All @@ -43,6 +43,7 @@
time: {
unit: 'day',
round: 'day',
tooltipFormat:'DD/MM/YYYY',
displayFormats: {
day: 'MMM D',
},
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def get_long_desc():

setuptools.setup(
name="drf_api_logger",
version="1.0.2",
version="1.0.3",
author="Vishal Anand",
author_email="[email protected]",
description="An API Logger for your Django Rest Framework project.",
Expand Down

0 comments on commit ef544cb

Please sign in to comment.