feat: improve Rich traceback rendering

This commit is contained in:
Matthias
2024-12-29 11:42:36 +01:00
parent 392486bc17
commit b6867d956c

View File

@@ -7,6 +7,11 @@ from rich.traceback import Traceback
class FtRichHandler(Handler):
"""
Basic colorized logging handler using Rich.
Does not support all features of the standard logging handler, and uses a hard-coded log format
"""
def __init__(self, console: Console, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._console = console
@@ -21,16 +26,15 @@ class FtRichHandler(Handler):
name = Text(record.name, style="violet")
log_level = Text(record.levelname, style=f"logging.level.{record.levelname.lower()}")
gray_sep = Text(" - ", style="gray46")
self._console.print(
Text() + log_time + gray_sep + name + gray_sep + log_level + gray_sep + msg
)
tb = None
if record.exc_info:
exc_type, exc_value, exc_traceback = record.exc_info
tb = Traceback.from_exception(exc_type, exc_value, exc_traceback)
self._console.print(Text() + log_time + gray_sep + name + gray_sep + log_level)
self._console.print(tb)
else:
# pass
self._console.print(
Text() + log_time + gray_sep + name + gray_sep + log_level + gray_sep + msg
)
self.flush()
except RecursionError: