You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I appreciate the work put into the drf-api-logger library, which has been incredibly helpful for logging API requests and responses in Django applications. I would like to propose an enhancement to make log handling more flexible and customizable, allowing users to specify their own logic for processing logs.
Current Behavior:
Currently, the InsertLogIntoDatabase class inserts logs directly into the database using the _insert_into_data_base method. This approach works well for many use cases, but it limits users who wish to send logs to external services, such as Seq or other logging solutions.
Proposed Enhancement:
Introduce a mechanism that allows users to provide a custom handler for processing logs. This could be achieved by defining a new setting in settings.py that specifies the path to a custom function or callable responsible for handling the logs. This would allow the default behavior of inserting logs into the database to remain unchanged while providing flexibility for users who wish to customize their log handling.
Implementation Details:
Custom Handler Setting:
Add a new setting in settings.py:
DRF_API_LOGGER_CUSTOM_HANDLER=None# Default to None, or provide a path to a custom function
Modify InsertLogIntoDatabase:
Update the InsertLogIntoDatabase class to check for the presence of the custom handler setting and use it if defined. Here is a brief outline of the changes:
fromimportlibimportimport_moduleclassInsertLogIntoDatabase(Thread):
# Existing initialization code...def__init__(self):
# Existing setup...self.custom_handler=getattr(settings, 'DRF_API_LOGGER_CUSTOM_HANDLER', None)
ifself.custom_handler:
self.custom_handler=self._import_custom_handler(self.custom_handler)
def_import_custom_handler(self, handler_path):
""" Import the custom handler function from a given string path. """module_path, func_name=handler_path.rsplit('.', 1)
module=import_module(module_path)
returngetattr(module, func_name)
def_insert_into_data_base(self, bulk_item):
""" Insert the bulk item into the database or use a custom handler if defined. """ifself.custom_handler:
try:
self.custom_handler(bulk_item)
exceptExceptionase:
print('DRF API LOGGER CUSTOM HANDLER EXCEPTION:', e)
else:
try:
APILogsModel.objects.using(self.DRF_API_LOGGER_DEFAULT_DATABASE).bulk_create(bulk_item)
exceptOperationalError:
raiseException(""" DRF API LOGGER EXCEPTION Model does not exist. Did you forget to migrate? """)
exceptExceptionase:
print('DRF API LOGGER EXCEPTION:', e)
Example Custom Handler:
Users can create their own handler functions, for example, to send logs to Seq:
Flexibility: Allows users to redirect logs to Seq or any other external logging service.
Backward Compatibility: Maintains existing behavior by default, ensuring no disruption for current users.
Configurability: Enhances the library's adaptability to various logging requirements without modifying core functionality.
I believe this enhancement would make the drf-api-logger library even more valuable by providing greater flexibility in how logs are handled. Please let me know if you have any questions or need further clarification on this proposal.
Thank you for considering this enhancement!
Best regards,
[Mohammad]
The text was updated successfully, but these errors were encountered:
Hello,
I appreciate the work put into the
drf-api-logger
library, which has been incredibly helpful for logging API requests and responses in Django applications. I would like to propose an enhancement to make log handling more flexible and customizable, allowing users to specify their own logic for processing logs.Current Behavior:
Currently, the
InsertLogIntoDatabase
class inserts logs directly into the database using the_insert_into_data_base
method. This approach works well for many use cases, but it limits users who wish to send logs to external services, such as Seq or other logging solutions.Proposed Enhancement:
Introduce a mechanism that allows users to provide a custom handler for processing logs. This could be achieved by defining a new setting in
settings.py
that specifies the path to a custom function or callable responsible for handling the logs. This would allow the default behavior of inserting logs into the database to remain unchanged while providing flexibility for users who wish to customize their log handling.Implementation Details:
Custom Handler Setting:
Add a new setting in
settings.py
:Modify
InsertLogIntoDatabase
:Update the
InsertLogIntoDatabase
class to check for the presence of the custom handler setting and use it if defined. Here is a brief outline of the changes:Example Custom Handler:
Users can create their own handler functions, for example, to send logs to Seq:
And then set it in
settings.py
:Benefits:
I believe this enhancement would make the
drf-api-logger
library even more valuable by providing greater flexibility in how logs are handled. Please let me know if you have any questions or need further clarification on this proposal.Thank you for considering this enhancement!
Best regards,
[Mohammad]
The text was updated successfully, but these errors were encountered: