DRF Debug
A package to tracking your API in django rest framework.
Usage
pip install drf-debug
INSTALLED_APPS = [
...
'tracking',
]
python manage.py migrate
add tracking.mixins.LoggingMixin to any DRF view to create a instance of APIRequestLog every time the view is called.
# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from tracking.mixins import LoggingMixin
class LoggingView(LoggingMixin, APIView):
def get(self, request):
return Response('with logging')
For performance enhancement, explicitly choose methods to be logged using logging_methods attribute:
class LoggingView(LoggingMixin, generics.CreateModelMixin, generics.GenericAPIView):
logging_methods = ['POST', 'PUT']
model = ...
Moreover, you could define your own rules by overriding should_log method. If should_log evaluates to True a log is created.
class LoggingView(LoggingMixin, generics.GenericAPIView):
def should_log(self, request, response):
"""Log only errors"""
return response.status_code >= 400
You could define your own handling. For example save on an in-memory data structure store, remote logging system etc.
class LoggingView(LoggingMixin, generics.GenericAPIView):
def handle_log(self, request, response):
cache.set('my_key', self.log, 86400)
Or you could omit save a request to the database. For example,
class LoggingView(LoggingMixin, generics.GenericAPIView):
def handle_log(self, request, response):
"""
Save only very slow requests. Requests that took more than a second.
"""
if self.log['response_ms'] > 1000:
super(LoggingMixin, self).handle_log(request, response)
By default drf-debug is hiding the values of those fields ['api', 'token', 'key', 'secret', 'password', 'signature'] You can complete this list with your own list by putting the fields you want to be hidden in the sensitive_fields parameter of your view.
class LoggingView(LoggingMixin, generics.CreateModelMixin, generics.GenericAPIView):
sensitive_fields = ['password', 'my_secret_field']
Links
Download Source Code: Click Here
My Github Account: Click Here
Release files for drf-debug 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| drf-debug-1.2.1.tar.gz | 7.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_debug-1.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.4 kB
Release files / drf-debug-1.2.1.tar.gz
| Download URL | drf-debug-1.2.1.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
befe33b2d303a12c55a1905e7ff56f7f73c03438e72500b61697e1864212ca60
|
|
BLAKE2b-256 checksum How to use checksums |
83b9f6e27d911a9ec2aac64f06ef52e7988f848a7009319a664d2a5cd806a38c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Release files / drf_debug-1.2.1-py3-none-any.whl
| Download URL | drf_debug-1.2.1-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a5b02c4fb88b7ee87870c2ec6c305263d9a55823bdd45ffb44c04536ba556086
|
|
BLAKE2b-256 checksum How to use checksums |
e8259144e99127eb7f303c781047c0b6cd0917c1b786facf68b1634746d14f41
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|