{"id":13415775,"url":"https://github.com/vishalanandl177/DRF-API-Logger","last_synced_at":"2025-03-14T23:31:06.351Z","repository":{"id":37719680,"uuid":"301002406","full_name":"vishalanandl177/DRF-API-Logger","owner":"vishalanandl177","description":"An API Logger for your Django Rest Framework project.","archived":false,"fork":false,"pushed_at":"2024-07-30T17:49:35.000Z","size":593,"stargazers_count":307,"open_issues_count":7,"forks_count":57,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-10-01T15:39:15.704Z","etag":null,"topics":["django-rest-framework","logger","logger-signals","python","python3"],"latest_commit_sha":null,"homepage":"https://github.com/vishalanandl177/DRF-API-Logger","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vishalanandl177.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-03T23:33:19.000Z","updated_at":"2024-10-01T14:40:54.000Z","dependencies_parsed_at":"2024-03-09T06:23:15.897Z","dependency_job_id":"d9cfeb3c-5e7b-4de8-a917-e8c90ce0b963","html_url":"https://github.com/vishalanandl177/DRF-API-Logger","commit_stats":{"total_commits":58,"total_committers":11,"mean_commits":"5.2727272727272725","dds":0.3275862068965517,"last_synced_commit":"457653e91ffb8ee40e2a799dcbd743cbbd5ebf92"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalanandl177%2FDRF-API-Logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalanandl177%2FDRF-API-Logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalanandl177%2FDRF-API-Logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishalanandl177%2FDRF-API-Logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vishalanandl177","download_url":"https://codeload.github.com/vishalanandl177/DRF-API-Logger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243663450,"owners_count":20327299,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django-rest-framework","logger","logger-signals","python","python3"],"created_at":"2024-07-30T21:00:52.009Z","updated_at":"2025-03-14T23:31:05.642Z","avatar_url":"https://github.com/vishalanandl177.png","language":"Python","funding_links":["https://paypal.me/chynybekov"],"categories":["Third-Party Packages","Python"],"sub_categories":["Logging"],"readme":"# DRF API Logger\n![version](https://img.shields.io/badge/version-1.1.16-blue.svg)\n[![Downloads](https://static.pepy.tech/personalized-badge/drf-api-logger?period=total\u0026units=none\u0026left_color=black\u0026right_color=orange\u0026left_text=Downloads%20Total)](http://pepy.tech/project/drf-api-logger)\n[![Downloads](https://static.pepy.tech/personalized-badge/drf-api-logger?period=month\u0026units=none\u0026left_color=black\u0026right_color=orange\u0026left_text=Downloads%20Last%20Month)](https://pepy.tech/project/drf-api-logger)\n[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)\n[![Donate](https://img.shields.io/badge/$-support-ff69b4.svg?style=flat)](https://paypal.me/chynybekov)  \n\n\u003ca href=\"https://discord.gg/eeYansFDCT\"\u003e\u003cimg src=\"https://img.shields.io/badge/Discord-7289DA?style=for-the-badge\u0026logo=discord\u0026logoColor=white\" alt=\"Join Community Badge\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://www.instagram.com/coderssecret/\"\u003e\u003cimg src=\"https://img.shields.io/badge/Instagram-E4405F?style=for-the-badge\u0026logo=instagram\u0026logoColor=white\" alt=\"Join Instagram\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/vishalanandl177\"\u003e\u003cimg src=\"https://img.shields.io/badge/GitHub-100000?style=for-the-badge\u0026logo=github\u0026logoColor=white\" alt=\"GitHub\"/\u003e\u003c/a\u003e\n\n\n\nAn API Logger for your Django Rest Framework project.\n\nIt logs all the API information for content type \"application/json\".\n1. URL\n2. Request Body\n3. Request Headers\n4. Request Method\n5. API Response\n6. Status Code\n7. API Call Time\n8. Server Execution Time\n9. Client IP Address\n\n\nYou can log API information into the database or listen to the logger signals for different use cases, or you can do both.\n\n* The logger uses a separate thread to run, so it won't affect your API response time.\n\n## Installation\n\nInstall or add drf-api-logger.\n```shell script\npip install drf-api-logger\n```\n\nAdd in INSTALLED_APPS\n```python\nINSTALLED_APPS = [\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n\n    'drf_api_logger',  #  Add here\n]\n```\n\nAdd in MIDDLEWARE\n```python\nMIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    'django.middleware.common.CommonMiddleware',\n    'django.middleware.csrf.CsrfViewMiddleware',\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    'django.contrib.messages.middleware.MessageMiddleware',\n    'django.middleware.clickjacking.XFrameOptionsMiddleware',\n\n    'drf_api_logger.middleware.api_logger_middleware.APILoggerMiddleware', # Add here\n]\n```\n\n\n#### * Add these lines in the Django Rest Framework settings file.\n\n## Store logs into the database\nLog every request into the database.\n```python\nDRF_API_LOGGER_DATABASE = True  # Default to False\n```\n* Logs will be available in the Django Admin Panel.\n\n* The search bar will search in Request Body, Response, Headers, and API URL.\n\n* You can also filter the logs based on the \"added_on\" date, Status Code, and Request Methods.\n\n![Alt text](https://raw.githubusercontent.com/vishalanandl177/DRF-API-Logger/master/logs.png?raw=true, \"Logger\")\n\n![Alt text](https://raw.githubusercontent.com/vishalanandl177/DRF-API-Logger/master/graph.png?raw=true, \"Graph\")\n\n![Alt text](https://raw.githubusercontent.com/vishalanandl177/DRF-API-Logger/master/lists.png?raw=true, \"Lists\")\n\n![Alt text](https://raw.githubusercontent.com/vishalanandl177/DRF-API-Logger/master/details.png?raw=true, \"Details\")\n\nNote: Make sure to migrate. It will create a table for the logger if \"DRF_API_LOGGER_DATABASE\" is True else if already exists, it will delete the table.\n\n## To listen for the logger signals.\nListen 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.\n```python\nDRF_API_LOGGER_SIGNAL = True  # Default to False\n```\nExample code to listen to the API Logger Signal.\n```python\n\"\"\"\nImport API_LOGGER_SIGNAL\n\"\"\"\nfrom drf_api_logger import API_LOGGER_SIGNAL\n\n\n\"\"\"\nCreate a function that is going to listen to the API logger signals.\n\"\"\"\ndef listener_one(**kwargs):\n    print(kwargs)\n\ndef listener_two(**kwargs):\n    print(kwargs)\n\n\"\"\"\nIt will listen to all the API logs whenever an API is called.\nYou can also listen to signals in multiple functions.\n\"\"\"\nAPI_LOGGER_SIGNAL.listen += listener_one\nAPI_LOGGER_SIGNAL.listen += listener_two\n\n\"\"\"\nUnsubscribe to signals.\n\"\"\"\n\nAPI_LOGGER_SIGNAL.listen -= listener_one\n```\n\n### Queue\n\nDRF API Logger usage queue to hold the logs before inserting them into the database. Once the queue is full, it bulk inserts into the database.\n\nSpecify the queue size.\n```python\nDRF_LOGGER_QUEUE_MAX_SIZE = 50  # Default to 50 if not specified.\n```\n\n### Interval\n\nDRF API Logger also waits for a period of time. If the queue is not full and there are some logs to be inserted, it inserts after the interval ends.\n\nSpecify an interval (In Seconds).\n```python\nDRF_LOGGER_INTERVAL = 10  # In Seconds, Default to 10 seconds if not specified.\n```\nNote: The API call time (added_on) is a timezone-aware datetime object. It is the actual time of the API call irrespective of interval value or queue size.\n### Skip namespace\nYou can skip the entire app to be logged into the database by specifying the namespace of the app as a list.\n```python\nDRF_API_LOGGER_SKIP_NAMESPACE = ['APP_NAMESPACE1', 'APP_NAMESPACE2']\n```\n\n### Skip URL Name\nYou can also skip any API to be logged by using the url_name of the API.\n```python\nDRF_API_LOGGER_SKIP_URL_NAME = ['url_name1', 'url_name2']\n```\n\nNote: It does not log Django Admin Panel API calls.\n\n### Hide Sensitive Data From Logs\nYou may wish to hide sensitive information from being exposed in the logs. \nYou do this by setting `DRF_API_LOGGER_EXCLUDE_KEYS` in settings.py to a list of your desired sensitive keys. \nThe default is\n```python\nDRF_API_LOGGER_EXCLUDE_KEYS = ['password', 'token', 'access', 'refresh']\n# Sensitive data will be replaced with \"***FILTERED***\".\n```\n\n### Change the default database to store API logs\n```python\nDRF_API_LOGGER_DEFAULT_DATABASE = 'default'  # Default to \"default\" if not specified\n\"\"\"\nMake sure to migrate the database specified in DRF_API_LOGGER_DEFAULT_DATABASE.\n\"\"\"\n```\n\n### Want to identify slow APIs? (Optional)\nYou can also identify slow APIs by specifying `DRF_API_LOGGER_SLOW_API_ABOVE` in settings.py.\n\nA new filter (By API Performance) will be visible, and you can choose a slow or fast API.\n```python\nDRF_API_LOGGER_SLOW_API_ABOVE = 200  # Default to None\n# Specify in milli-seconds.\n```\n\n### Want to log only selected request methods? (Optional)\nYou can log only selected methods by specifying `DRF_API_LOGGER_METHODS` in settings.py.\n```python\nDRF_API_LOGGER_METHODS = ['GET', 'POST', 'DELETE', 'PUT']  # Default to an empty list (Log all the requests).\n```\n\n### Want to log only selected response status codes? (Optional)\nYou can log only selected responses by specifying `DRF_API_LOGGER_STATUS_CODES` in settings.py.\n```python\nDRF_API_LOGGER_STATUS_CODES = [200, 400, 404, 500]  # Default to an empty list (Log all responses).\n```\n\n### Want to log custom content types? (Optional)\nYou can log custom content types by specifying `DRF_API_LOGGER_CONTENT_TYPES` in settings.py.\nThe specified content types will be added alongside with default content types.\n```python\nDRF_API_LOGGER_CONTENT_TYPES = [\n    \"application/json\",\n    \"application/vnd.api+json\",\n    \"application/gzip\",\n    \"application/octet-stream\",\n]  # Default content types.\n```\n\n### Want to see the API information in the local timezone? (Optional)\nYou can also change the timezone by specifying `DRF_API_LOGGER_TIMEDELTA` in settings.py.\nIt won't change the Database timezone. It will remain UTC or the timezone you have defined.\n```python\nDRF_API_LOGGER_TIMEDELTA = 330 # UTC + 330 Minutes = IST (5:Hours, 30:Minutes ahead from UTC) \n# Specify in minutes.\n```\n```python\n# You can specify negative values for the countries behind the UTC timezone.\nDRF_API_LOGGER_TIMEDELTA = -30  # Example\n```\n\n### Ignore data based on maximum request or response body? (Optional)\nRequest/Response bodies\nBy default, DRF API LOGGER will save the request and response bodies for each request for future viewing\nno matter how large. If DRF API LOGGER is used in production under heavy volume with\nlarge bodies this can have a huge impact on space/time performance.\n\nThis behavior can be configured with the following options additional:\n```python\n# DRF API LOGGER takes anything \u003c 0 as no limit.\n# If response body \u003e 1024 bytes, ignore.\nDRF_API_LOGGER_MAX_REQUEST_BODY_SIZE = 1024  # default to -1, no limit.\nDRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE = 1024  # default to -1, no limit.\n```\n\n### Tracing\nYou can enable tracing by specifying `DRF_API_LOGGER_ENABLE_TRACING` in settings.py.\nThis will add a tracing ID (UUID.uuid4()) in the signals of the DRF API Logger (if enabled).\n\nIn views, you can use request.tracing_id to get the tracing ID.\n```python\nDRF_API_LOGGER_ENABLE_TRACING = True  # default to False\n```\n\n### Want to generate your tracing uuid?\nBy default, the DRF API Logger uses uuid.uuid4() to generate tracing id.\nIf you want to use your custom function to generate uuid, specify DRF_API_LOGGER_TRACING_FUNC in the setting.py file.\n```python\nDRF_API_LOGGER_TRACING_FUNC = 'foo.bar.func_name'\n```\n\n### Tracing already present in headers?\nIf the tracing ID is already coming as a part of request headers, you can specify the header name.\n```python\nDRF_API_LOGGER_TRACING_ID_HEADER_NAME: str = 'X_TRACING_ID'  # Replace with actual header name.\n```\n\n### API with or without Host\nYou can specify whether an endpoint of API should have absolute URI or not by setting this variable in the DRF settings.py file.\n```python\nDRF_API_LOGGER_PATH_TYPE = 'ABSOLUTE'  # Default to ABSOLUTE if not specified\n# Possible values are ABSOLUTE, FULL_PATH or RAW_URI\n```\n\nConsidering we are accessing the following URL: http://127.0.0.1:8000/api/v1/?page=123\nDRF_API_LOGGER_PATH_TYPE possible values are:\n1. ABSOLUTE (Default) :   \n\n    Function used ```request.build_absolute_uri()```\n    \n    Output: ```http://127.0.0.1:8000/api/v1/?page=123```\n    \n2. FULL_PATH\n\n    Function used ```request.get_full_path()```\n    \n    Output: ```/api/v1/?page=123```\n    \n3. RAW_URI\n\n    Function used ```request.get_raw_uri()```\n    \n    Output: ```http://127.0.0.1:8000/api/v1/?page=123```\n    \n    Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return an insecure URI.\n\n\n### Use the DRF API Logger Model to query \nYou can use the DRF API Logger Model to query some information.\n\nNote: Make sure to set \"DRF_API_LOGGER_DATABASE = True\" in the settings.py file.\n```python\nfrom drf_api_logger.models import APILogsModel\n\n\"\"\"\nExample:\nSelect records for status_code 200.\n\"\"\"\n\nresult_for_200_status_code = APILogsModel.objects.filter(status_code=200)\n```\n\nDRF API Logger Model:\n```python\nclass APILogsModel(Model):\n   id = models.BigAutoField(primary_key=True)\n   api = models.CharField(max_length=1024, help_text='API URL')\n   headers = models.TextField()\n   body = models.TextField()\n   method = models.CharField(max_length=10, db_index=True)\n   client_ip_address = models.CharField(max_length=50)\n   response = models.TextField()\n   status_code = models.PositiveSmallIntegerField(help_text='Response status code', db_index=True)\n   execution_time = models.DecimalField(decimal_places=5, max_digits=8,\n                                       help_text='Server execution time (Not complete response time.)')\n   added_on = models.DateTimeField()\n   \n   def __str__(self):\n      return self.api\n\n   class Meta:\n      db_table = 'drf_api_logs'\n      verbose_name = 'API Log'\n      verbose_name_plural = 'API Logs'\n\n```\n\n### Note:\nAfter some time, there will be too much data in the database. Searching and filtering may get slower.\nIf you want, you can delete or archive the older data.\nTo improve the searching or filtering, try to add indexes in the 'drf_api_logs' table.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvishalanandl177%2FDRF-API-Logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvishalanandl177%2FDRF-API-Logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvishalanandl177%2FDRF-API-Logger/lists"}