Open vikramsubramanian opened 3 months ago
To implement the feature request described in the Jira ticket, you will need to modify the loguru
library's logging methods to allow passing an exception directly to the logger.exception()
method. Here's a high-level overview of the steps to implement this feature:
ErrorInterceptor
class to handle a passed exception._log
method in the _logger.py
file to accept an exception parameter.exception
method in the _logger.py
file to handle the new parameter.Here's a more detailed breakdown:
Update the ErrorInterceptor
class to handle a passed exception:
class ErrorInterceptor:
# ... existing code ...
def print(self, record=None, *, exception=None):
# ... existing code ...
if exception is not None:
type_, value, traceback_ = (type(exception), exception, exception.__traceback__)
else:
type_, value, traceback_ = sys.exc_info()
# ... existing code ...
Modify the _log
method in the _logger.py
file to accept an exception
parameter and pass it to the ErrorInterceptor
:
def _log(self, level, msg, *args, exception=None, **kwargs):
# ... existing code ...
if exception is not None:
options = (exception,) + self._options[1:]
else:
options = self._options
# ... existing code ...
Update the exception
method in the _logger.py
file to accept the exception
parameter and pass it to the _log
method:
def exception(self, msg, *args, exception=None, **kwargs):
self._log("ERROR", msg, *args, exception=exception, **kwargs)
Ensure that the exception
method is exposed in the logger's interface so that it can be called with the new parameter.
After making these changes, you should be able to call logger.exception()
with an exception object, and it will log the stack trace of the passed exception instead of the current exception from sys.exc_info()
.
Please note that this is a high-level overview, and you will need to ensure that the changes are compatible with the rest of the loguru
library's codebase. Additionally, you should write unit tests to cover the new functionality and ensure that existing functionality is not broken by the changes.
In asyncio callbacks, i.e.: a Starlette error handler, we have the exception but it's not part of the
sys.exc_info()
. Therefore I can't log the stacktrace vialogger.exception()
.It'd be great if I could pass the exception in, i.e.:
Right now the workaround is to use private options and functions
)