DRF-API-Logger
DRF-API-Logger copied to clipboard
DRF_API_LOGGER_CUSTOM_HANDLER is imported but never called in put_log_data
Bug Description
The DRF_API_LOGGER_CUSTOM_HANDLER setting is documented and the handler is imported in InsertLogIntoDatabase.__init__(), but it is never actually called anywhere in the code.
Expected Behavior
The custom handler should be called in put_log_data() to allow users to enrich/modify log data before it's saved to the database.
Current Behavior
In insert_log_into_database.py, the custom handler is loaded:
def __init__(self):
# ...
self.custom_handler = getattr(settings, 'DRF_API_LOGGER_CUSTOM_HANDLER', None)
if self.custom_handler:
self.custom_handler = self._import_custom_handler(self.custom_handler)
But in put_log_data(), it's never used:
def put_log_data(self, data):
self._queue.put(APILogsModel(**data)) # custom_handler not called!
Suggested Fix
def put_log_data(self, data):
if self.custom_handler:
data = self.custom_handler(data)
self._queue.put(APILogsModel(**data))
Version drf-api-logger: 1.1.21
Workaround Currently using a monkey-patch in Django's AppConfig.ready():
def ready(self):
from drf_api_logger.apps import LOGGER_THREAD
if LOGGER_THREAD and LOGGER_THREAD.custom_handler:
original = LOGGER_THREAD.put_log_data
def patched(data):
data = LOGGER_THREAD.custom_handler(data)
return original(data)
LOGGER_THREAD.put_log_data = patched