Package: drf-api-logger
DRF API LoggerAn API Logger for your Django Rest Framework project.It logs all the API information for content type "application/json".URLRequest BodyRequest HeadersRequest MethodAPI ResponseStatus CodeAPI Call TimeServer Execution TimeClient IP AddressYou 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.InstallationInstall or add drf-api-logger.pipinstalldrf-api-loggerAdd in INSTALLED_APPSINSTALLED_APPS=['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','drf_api_logger',#  Add here]Add in MIDDLEWAREMIDDLEWARE=['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware','drf_api_logger.middleware.api_logger_middleware.APILoggerMiddleware',# Add here]* Add these lines in Django Rest Framework settings file.Store logs into the databaseLog every request into the database.DRF_API_LOGGER_DATABASE=True# Default to FalseLogs 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.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 FalseExample code to listen to the API Logger Signal."""Import API_LOGGER_SIGNAL"""fromdrf_api_loggerimportAPI_LOGGER_SIGNAL"""Create a function that is going to listen to the API logger signals."""deflistener_one(**kwargs):print(kwargs)deflistener_two(**kwargs):print(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_oneAPI_LOGGER_SIGNAL.listen+=listener_two"""Unsubscribe to signals."""API_LOGGER_SIGNAL.listen-=listener_oneQueueDRF 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.IntervalDRF 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 an interval (In Seconds).DRF_LOGGER_INTERVAL=10# In Seconds, Default to 10 seconds if not specified.Note: The API call time (added_on) is a timezone aware datetime object. It is actual time of API call irrespective of interval value or queue size.Skip namespaceYou can skip the entire app to be logged into the database by specifying namespace of the app as list.DRF_API_LOGGER_SKIP_NAMESPACE=['APP_NAMESPACE1','APP_NAMESPACE2']Skip URL NameYou 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.Hide Sensitive Data From LogsYou may wish to hide sensitive information from being exposed in the logs.
You do this by settingDRF_API_LOGGER_EXCLUDE_KEYSin settings.py to a list of your desired sensitive keys.
The default isDRF_API_LOGGER_EXCLUDE_KEYS=['password','token','access','refresh']# Sensitive data will be replaced with "***FILTERED***".Change default database to store API logsDRF_API_LOGGER_DEFAULT_DATABASE='default'# Default to "default" if not specified"""Make sure to migrate the database specified in DRF_API_LOGGER_DEFAULT_DATABASE."""Want to identify slow APIs? (Optional)You can also identify slow APIs by specifyingDRF_API_LOGGER_SLOW_API_ABOVEin settings.py.A new filter (By API Performance) will be visible, and you can choose slow or fast API.DRF_API_LOGGER_SLOW_API_ABOVE=200# Default to None# Specify in milli-seconds.Want to log only selected request methods? (Optional)You can log only selected methods by specifyingDRF_API_LOGGER_METHODSin settings.py.DRF_API_LOGGER_METHODS=['GET','POST','DELETE','PUT']# Default to empty list (Log all the requests).Want to log only selected response status codes? (Optional)You can log only selected responses by specifyingDRF_API_LOGGER_STATUS_CODESin settings.py.DRF_API_LOGGER_STATUS_CODES=[200,400,404,500]# Default to empty list (Log all responses).Want to see the API information in local timezone? (Optional)You can also change the timezone by specifyingDRF_API_LOGGER_TIMEDELTAin settings.py.
It won't change the Database timezone. It will still remain UTC or the timezone you have defined.DRF_API_LOGGER_TIMEDELTA=330# UTC + 330 Minutes = IST (5:Hours, 30:Minutes ahead from UTC)# Specify in minutes.# Yoc can specify negative values for the countries behind the UTC timezone.DRF_API_LOGGER_TIMEDELTA=-30# ExampleIgnore data based on maximum request or response body? (Optional)Request/Response bodies
By default, DRF API LOGGER will save down the request and response bodies for each request for future viewing
no matter how large. If DRF API LOGGER is used in production under heavy volume with
large bodies this can have a huge impact on space/time performance.This behaviour can be configured with the following options additional:# DRF API LOGGER takes anything < 0 as no limit.# If response body > 1024 bytes, ignore.DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE=1024# default to -1, no limit.DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE=1024# default to -1, no limit.API with or without HostYou can specify an endpoint of API should have absolute URI or not by setting this variable in DRF settings.py file.DRF_API_LOGGER_PATH_TYPE='ABSOLUTE'# Default to ABSOLUTE if not specified# Possible values are ABSOLUTE, FULL_PATH or RAW_URITracingYou can enable tracing by specifyingDRF_API_LOGGER_ENABLE_TRACINGin settings.py.
This will add a tracing id (UUID.uuid4()) in the signals of DRF API Logger (if enabled).In views, you can use request.tracing_id to get the tracing id.DRF_API_LOGGER_ENABLE_TRACING=True# default to FalseWant to generate your own tracing uuid?By default, DRF API Logger usage uuid.uuid4() to generate tracing id.
If you want to use your custom function to generate uuid, specify DRF_API_LOGGER_TRACING_FUNC in setting.py file.DRF_API_LOGGER_TRACING_FUNC='foo.bar.func_name'Tracing already present in headers?If the tracing id is already coming as a part of request headers, you can specify the header name.DRF_API_LOGGER_TRACING_ID_HEADER_NAME:str='X_TRACING_ID'# Replace with actual header name.Considering we are accessing the following URL:http://127.0.0.1:8000/api/v1/?page=123DRF_API_LOGGER_PATH_TYPE possible values are:ABSOLUTE (Default) :Function usedrequest.build_absolute_uri()Output:http://127.0.0.1:8000/api/v1/?page=123FULL_PATHFunction usedrequest.get_full_path()Output:/api/v1/?page=123RAW_URIFunction usedrequest.get_raw_uri()Output:http://127.0.0.1:8000/api/v1/?page=123Note: Similar to ABSOLUTE but skip allowed hosts protection, so may return an insecure URI.Use DRF API Logger Model to queryYou can use the DRF API Logger Model to query some information.Note: Make sure to set "DRF_API_LOGGER_DATABASE = True" in settings.py file.fromdrf_api_logger.modelsimportAPILogsModel"""Example:Select records for status_code 200."""result_for_200_status_code=APILogsModel.objects.filter(status_code=200)DRF API Logger Model:classAPILogsModel(Model):id=models.BigAutoField(primary_key=True)api=models.CharField(max_length=1024,help_text='API URL')headers=models.TextField()body=models.TextField()method=models.CharField(max_length=10,db_index=True)client_ip_address=models.CharField(max_length=50)response=models.TextField()status_code=models.PositiveSmallIntegerField(help_text='Response status code',db_index=True)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):returnself.apiclassMeta: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.