Skip to main content

An API Logger for your Django Rest Framework project.

Project description

DRF API Logger

version Downloads Downloads Open Source GitHub issues


An API Logger for your Django Rest Framework project.

It logs all the API information for content type "application/json".

  1. URL
  2. Request Body
  3. Request Headers
  4. Request Method
  5. API Response
  6. Status Code
  7. API Call Time
  8. Server Execution Time
  9. Client IP Address

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

  • The logger usage a separate thread to run so it won't affect your API response time.


Install or add drf-api-logger.

pip install drf-api-logger



    'drf_api_logger',  #  Add here



    'drf_api_logger.middleware.api_logger_middleware.APILoggerMiddleware', # Add here

* Add these lines in Django Rest Framework settings file.

Store logs into the database

Log every request into the database.

DRF_API_LOGGER_DATABASE = True  # Default to False
  • Logs will be available in Django Admin Panel.

  • The search bar will search in Request Body, Response, Headers and API URL.

  • You can also filter the logs based on the "added_on" date, Status Code and Request Methods.

Alt text

Alt text

Alt text

Note: Make sure to migrate. It will create a table for logger if "DRF_API_LOGGER_DATABASE" is True else if already exists, it will delete the table.

To listen for the logger signals.

Listen to the signal as soon as any API is called. So you can log the API data into a file or for different use-cases.

DRF_API_LOGGER_SIGNAL = True  # Default to False

Example code to listen to the API Logger Signal.

from drf_api_logger import API_LOGGER_SIGNAL

Create a function that is going to listen to the API logger signals.
def listener_one(**kwargs):

def listener_two(**kwargs):

It will listen to all the API logs whenever an API is called.
You can also listen signals in multiple functions.
API_LOGGER_SIGNAL.listen += listener_one
API_LOGGER_SIGNAL.listen += listener_two

Unsubscribe to signals.

API_LOGGER_SIGNAL.listen -= listener_one


DRF API Logger usage queue to hold the logs before inserting into the database. Once queue is full, it bulk inserts into the database.

Specify the queue size.

DRF_LOGGER_QUEUE_MAX_SIZE = 50  # Default to 50 if not specified.


DRF API Logger also waits for a period of time. If queue is not full and there are some logs to be inserted, it inserts after interval ends.

Specify interval (In Seconds).

DRF_LOGGER_INTERVAL = 10  # In Seconds, Default to 10 seconds if not specified.

Note: The API call time (added_on) is timezone aware datetime object. It is actual time of API call irrespective of interval value or queue size.

Skip namespace

You can skip the entire app to be logged into the database by specifying namespace of the app as list.


Skip URL Name

You can also skip any API to be logged by using url_name of the API.

DRF_API_LOGGER_SKIP_URL_NAME = ['url_name1', 'url_name2']

Note: It does not log Django Admin Panel API calls.

API with or without Host

You can specify endpoint of API should have absolute URI or not by setting this variable in DRF file.

DRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE'  # Default to ABSOLUTE if not specified
# Possible values are ABSOLUTE, FULL_PATH or RAW_URI

Considering we are accessing the following URL: DRF_API_LOGGER_PATH_TYPE possible values are:

  1. ABSOLUTE (Default) :

    Function used request.build_absolute_uri()



    Function used request.get_full_path()

    Output: /api/v1/?page=123

  3. RAW_URI

    Function used request.get_raw_uri()


    Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return insecure URI.

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_api_logger-0.0.9.tar.gz (8.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page