Skip to content

Commit bf27fb8

Browse files
Now we can specify any database to store logs.
1 parent a841006 commit bf27fb8

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

README.md

Lines changed: 26 additions & 2 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.6-blue.svg)
2+
![version](https://img.shields.io/badge/version-1.0.7-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/)
@@ -156,9 +156,20 @@ DRF_API_LOGGER_SKIP_URL_NAME = ['url_name1', 'url_name2']
156156
Note: It does not log Django Admin Panel API calls.
157157

158158
### Hide Sensitive Data From Logs
159-
You may wish to hide sensitive information from being exposed in the logs. You do this by setting `DRF_API_LOGGER_EXCLUDE_KEYS` in settings.py to a list of your desired sensitive keys. The default is
159+
You may wish to hide sensitive information from being exposed in the logs.
160+
You do this by setting `DRF_API_LOGGER_EXCLUDE_KEYS` in settings.py to a list of your desired sensitive keys.
161+
The default is
160162
```python
161163
DRF_API_LOGGER_EXCLUDE_KEYS = ['password', 'token', 'access', 'refresh']
164+
# Sensitive data will be replaced with "***FILTERED***".
165+
```
166+
167+
### Change default database to store API logs
168+
```python
169+
DRF_API_LOGGER_DEFAULT_DATABASE = 'default' # Default to "default" if not specified
170+
"""
171+
Make sure to migrate the database specified in DRF_API_LOGGER_DEFAULT_DATABASE.
172+
"""
162173
```
163174

164175
### API with or without Host
@@ -218,5 +229,18 @@ class APILogsModel(Model):
218229
execution_time = models.DecimalField(decimal_places=5, max_digits=8,
219230
help_text='Server execution time (Not complete response time.)')
220231
added_on = models.DateTimeField()
232+
233+
def __str__(self):
234+
return self.api
235+
236+
class Meta:
237+
db_table = 'drf_api_logs'
238+
verbose_name = 'API Log'
239+
verbose_name_plural = 'API Logs'
221240
222241
```
242+
243+
### Note:
244+
After sometime, there will be too many data in the database. Searching and filter may get slower.
245+
If you want, you can delete or archive the older data.
246+
To improve the searching or filtering, try to add indexes in the 'drf_api_logs' table.

drf_api_logger/insert_log_into_database.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class InsertLogIntoDatabase(Thread):
1111

1212
def __init__(self):
1313
super().__init__()
14+
15+
self.DRF_API_LOGGER_DEFAULT_DATABASE = 'default'
16+
if hasattr(settings, 'DRF_API_LOGGER_DEFAULT_DATABASE'):
17+
self.DRF_API_LOGGER_DEFAULT_DATABASE = settings.DRF_API_LOGGER_DEFAULT_DATABASE
18+
1419
self.DRF_LOGGER_QUEUE_MAX_SIZE = 50 # Default queue size 50
1520
if hasattr(settings, 'DRF_LOGGER_QUEUE_MAX_SIZE'):
1621
self.DRF_LOGGER_QUEUE_MAX_SIZE = settings.DRF_LOGGER_QUEUE_MAX_SIZE
@@ -56,7 +61,7 @@ def _start_bulk_insertion(self):
5661

5762
def _insert_into_data_base(self, bulk_item):
5863
try:
59-
APILogsModel.objects.bulk_create(bulk_item)
64+
APILogsModel.objects.using(self.DRF_API_LOGGER_DEFAULT_DATABASE).bulk_create(bulk_item)
6065
except OperationalError:
6166
raise Exception("""
6267
DRF API LOGGER EXCEPTION

drf_api_logger/middleware/api_logger_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __call__(self, request):
8181

8282
headers = get_headers(request=request)
8383
method = request.method
84-
if 'content-type' in response and response['content-type'] == 'application/json' or response['content-type'] == 'application/vnd.api+json':
84+
if response.get('content-type') in ('application/json', 'application/vnd.api+json',):
8585
if getattr(response, 'streaming', False):
8686
response_body = '** Streaming **'
8787
else:

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.6",
24+
version="1.0.7",
2525
author="Vishal Anand",
2626
author_email="vishalanandl177@gmail.com",
2727
description="An API Logger for your Django Rest Framework project.",

0 commit comments

Comments
 (0)