feat: optional RichHandler keywords

This commit is contained in:
Aaron Stephens 2022-02-04 13:25:56 -08:00
parent 880693db5b
commit bdd1fd77ab
2 changed files with 7 additions and 1 deletions

View File

@ -25,5 +25,6 @@ The following people have contributed to the development of Rich:
- [Tushar Sadhwani](https://github.com/tusharsadhwani)
- [Tim Savage](https://github.com/timsavage)
- [Nicolas Simonds](https://github.com/0xDEC0DE)
- [Aaron Stephens](https://github.com/aaronst)
- [Gabriele N. Tornetta](https://github.com/p403n1x87)
- [Patrick Arminio](https://github.com/patrick91)

View File

@ -41,6 +41,7 @@ class RichHandler(Handler):
Defaults to 10.
locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.
log_time_format (Union[str, TimeFormatterCallable], optional): If ``log_time`` is enabled, either string for strftime or callable that formats the time. Defaults to "[%x %X] ".
keywords (List[str], optional): List of words to highlight instead of ``RichHandler.KEYWORDS``.
"""
KEYWORDS: ClassVar[Optional[List[str]]] = [
@ -76,6 +77,7 @@ class RichHandler(Handler):
locals_max_length: int = 10,
locals_max_string: int = 80,
log_time_format: Union[str, FormatTimeCallable] = "[%x %X]",
keywords: Optional[List[str]] = None
) -> None:
super().__init__(level=level)
self.console = console or get_console()
@ -98,6 +100,7 @@ class RichHandler(Handler):
self.tracebacks_show_locals = tracebacks_show_locals
self.locals_max_length = locals_max_length
self.locals_max_string = locals_max_string
self.keywords = keywords
def get_level_text(self, record: LogRecord) -> Text:
"""Get the level name from the record.
@ -171,7 +174,9 @@ class RichHandler(Handler):
if highlighter:
message_text = highlighter(message_text)
if self.KEYWORDS:
if self.keywords:
message_text.highlight_words(self.keywords, "logging.keyword")
elif self.KEYWORDS:
message_text.highlight_words(self.KEYWORDS, "logging.keyword")
return message_text