Skip to main content

API tracking package.

Project description

DRF Debug

A package to tracking your API in django rest framework.

Usage

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-debug-1.2.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

drf_debug-1.2.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file drf-debug-1.2.0.tar.gz.

File metadata

  • Download URL: drf-debug-1.2.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for drf-debug-1.2.0.tar.gz
Algorithm Hash digest
SHA256 960b5d6f7fca663a93b78f18913004cbdaecff738bcb01f59b5146ef2028ca23
MD5 ba2ec324872867872161b9a91d173e83
BLAKE2b-256 6494c20bc9f0dbd2e11e271e1187a108613b8324418075f5148942fe1038401f

See more details on using hashes here.

File details

Details for the file drf_debug-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: drf_debug-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for drf_debug-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1dcc9d877bfde99b7071403ac2fab3f041d1b6fb837059fd6ccca3e93ea0c84e
MD5 4f022c129c6e7abb8fd79227345c2b6a
BLAKE2b-256 83a8c19ec3322f6fe29ed3b6b456c69b6e2f4655f3344423e7917959462d7ee4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page