From bdd1fd77ab6f9bec289f6a69f66ca734d22b6589 Mon Sep 17 00:00:00 2001 From: Aaron Stephens Date: Fri, 4 Feb 2022 13:25:56 -0800 Subject: [PATCH] feat: optional RichHandler keywords --- CONTRIBUTORS.md | 1 + rich/logging.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c29cd790..4508d9a6 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -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) diff --git a/rich/logging.py b/rich/logging.py index 002f1f7b..7f7b499a 100644 --- a/rich/logging.py +++ b/rich/logging.py @@ -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