Skip to content

Commit

Permalink
Now we can specify any database to store logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalanandl177 committed Jun 4, 2021
1 parent a841006 commit bf27fb8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
28 changes: 26 additions & 2 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.6-blue.svg)
![version](https://img.shields.io/badge/version-1.0.7-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 Down Expand Up @@ -156,9 +156,20 @@ DRF_API_LOGGER_SKIP_URL_NAME = ['url_name1', 'url_name2']
Note: It does not log Django Admin Panel API calls.

### Hide Sensitive Data From Logs
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
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
```python
DRF_API_LOGGER_EXCLUDE_KEYS = ['password', 'token', 'access', 'refresh']
# Sensitive data will be replaced with "***FILTERED***".
```

### Change default database to store API logs
```python
DRF_API_LOGGER_DEFAULT_DATABASE = 'default' # Default to "default" if not specified
"""
Make sure to migrate the database specified in DRF_API_LOGGER_DEFAULT_DATABASE.
"""
```

### API with or without Host
Expand Down Expand Up @@ -218,5 +229,18 @@ class APILogsModel(Model):
execution_time = models.DecimalField(decimal_places=5, max_digits=8,
help_text='Server execution time (Not complete response time.)')
added_on = models.DateTimeField()
def __str__(self):
return self.api
class Meta:
db_table = 'drf_api_logs'
verbose_name = 'API Log'
verbose_name_plural = 'API Logs'
```

### Note:
After sometime, there will be too many data in the database. Searching and filter may get slower.
If you want, you can delete or archive the older data.
To improve the searching or filtering, try to add indexes in the 'drf_api_logs' table.
7 changes: 6 additions & 1 deletion drf_api_logger/insert_log_into_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class InsertLogIntoDatabase(Thread):

def __init__(self):
super().__init__()

self.DRF_API_LOGGER_DEFAULT_DATABASE = 'default'
if hasattr(settings, 'DRF_API_LOGGER_DEFAULT_DATABASE'):
self.DRF_API_LOGGER_DEFAULT_DATABASE = settings.DRF_API_LOGGER_DEFAULT_DATABASE

self.DRF_LOGGER_QUEUE_MAX_SIZE = 50 # Default queue size 50
if hasattr(settings, 'DRF_LOGGER_QUEUE_MAX_SIZE'):
self.DRF_LOGGER_QUEUE_MAX_SIZE = settings.DRF_LOGGER_QUEUE_MAX_SIZE
Expand Down Expand Up @@ -56,7 +61,7 @@ def _start_bulk_insertion(self):

def _insert_into_data_base(self, bulk_item):
try:
APILogsModel.objects.bulk_create(bulk_item)
APILogsModel.objects.using(self.DRF_API_LOGGER_DEFAULT_DATABASE).bulk_create(bulk_item)
except OperationalError:
raise Exception("""
DRF API LOGGER EXCEPTION
Expand Down
2 changes: 1 addition & 1 deletion drf_api_logger/middleware/api_logger_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __call__(self, request):

headers = get_headers(request=request)
method = request.method
if 'content-type' in response and response['content-type'] == 'application/json' or response['content-type'] == 'application/vnd.api+json':
if response.get('content-type') in ('application/json', 'application/vnd.api+json',):
if getattr(response, 'streaming', False):
response_body = '** Streaming **'
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_long_desc():

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

0 comments on commit bf27fb8

Please sign in to comment.