Skip to content

Commit ef544cb

Browse files
Improvements and model usage.
1 parent d186aaf commit ef544cb

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

README.md

Lines changed: 37 additions & 5 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.2-blue.svg)
2+
![version](https://img.shields.io/badge/version-1.0.3-blue.svg)
33
[![Downloads](https://pepy.tech/badge/drf-api-logger)](http://pepy.tech/project/drf-api-logger)
44
[![Downloads](https://pepy.tech/badge/drf-api-logger/month)](https://pepy.tech/project/drf-api-logger)
55
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
@@ -23,9 +23,9 @@ It logs all the API information for content type "application/json".
2323
9. Client IP Address
2424

2525

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

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

3030
## Installation
3131

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

156156
### API with or without Host
157-
You can specify endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
157+
You can specify an endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.
158158
```python
159159
DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE' # Default to ABSOLUTE if not specified
160160
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI
@@ -179,4 +179,36 @@ DRF_API_LOGGER_PATH_TYPE possible values are:
179179

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

182-
Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return insecure URI.
182+
Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return an insecure URI.
183+
184+
185+
### Use DRF API Logger Model to query
186+
You can use the DRF API Logger Model to query some information.
187+
188+
Note: Make sure to set "DRF_API_LOGGER_DATABASE = True" in settings.py file.
189+
```
190+
from drf_api_logger.models import APILogsModel
191+
192+
"""
193+
Example:
194+
Select records for status_code 200.
195+
"""
196+
result_for_200_status_code = APILogsModel.objects.filter(status_code=200)
197+
```
198+
199+
Model:
200+
```
201+
class APILogsModel(Model):
202+
id = models.BigAutoField(primary_key=True)
203+
api = models.CharField(max_length=512, help_text='API URL')
204+
headers = models.TextField()
205+
body = models.TextField()
206+
method = models.CharField(max_length=10, db_index=True)
207+
client_ip_address = models.CharField(max_length=50)
208+
response = models.TextField()
209+
status_code = models.PositiveSmallIntegerField(help_text='Response status code', db_index=True)
210+
execution_time = models.DecimalField(decimal_places=5, max_digits=8,
211+
help_text='Server execution time (Not complete response time.)')
212+
added_on = models.DateTimeField()
213+
214+
```

drf_api_logger/admin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from django.conf.urls import url
21
from django.contrib import admin
32
from django.db.models import Count
4-
from django.shortcuts import render
5-
from django.views import View
3+
64

75
from drf_api_logger.utils import database_log_enabled
86

@@ -46,4 +44,7 @@ def has_add_permission(self, request, obj=None):
4644
def has_change_permission(self, request, obj=None):
4745
return False
4846

47+
def has_delete_permission(self, request, obj=None):
48+
return False
49+
4950
admin.site.register(APILogsModel, APILogsAdmin)

drf_api_logger/insert_log_into_database.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ def _insert_into_data_base(self, bulk_item):
6363
Model does not exists.
6464
Did you forget to migrate?
6565
""")
66+
except Exception as e:
67+
print('DRF API LOGGER EXCEPTION:', e)

drf_api_logger/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from drf_api_logger.utils import database_log_enabled
44

5+
56
if database_log_enabled():
67
"""
78
Load models only if DRF_API_LOGGER_DATABASE is True

drf_api_logger/templates/charts_change_list.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{
3131
label: 'Number of API calls',
3232
data: chartData,
33-
backgroundColor: 'rgba(220,20,20,0.5)',
33+
backgroundColor: 'rgb(33,150,243)',
3434
},
3535
],
3636
},
@@ -43,6 +43,7 @@
4343
time: {
4444
unit: 'day',
4545
round: 'day',
46+
tooltipFormat:'DD/MM/YYYY',
4647
displayFormats: {
4748
day: 'MMM D',
4849
},

setup.py

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

1010
setuptools.setup(
1111
name="drf_api_logger",
12-
version="1.0.2",
12+
version="1.0.3",
1313
author="Vishal Anand",
1414
author_email="vishalanandl177@gmail.com",
1515
description="An API Logger for your Django Rest Framework project.",

0 commit comments

Comments
 (0)