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 want all ERROR messages to the console to be in red. I'm not talking about the level text, but the actual message. I'm completely new to rich. Looking at the code for RichHandler, it seems like a custom Highlighter is the best approach. Is there a better method?
I used a filter to change the highlighter on a given LogRecord depending on its level.
def_build_stdout_handler() ->logging.Handler:
fromrich.loggingimportRichHandler, Textfromrich.highlighterimportHighlighter, NullHighlighterclassRedHighlighter(Highlighter):
defhighlight(self, text: Text) ->None:
text.style="bold red"defcolored_error_filter(record: logging.LogRecord) -> (bool|logging.LogRecord):
ifrecord.levelno==logging.ERROR:
record.highlighter=RedHighlighter()
returnrecordreturnTrue# RichHandler defaults to stdout# I don't want any other highlighting or special output.stdout_handler=RichHandler(show_time=False, show_level=False, show_path=False, highlighter=NullHighlighter())
stdout_handler.addFilter(colored_error_filter)
# the old handler that I'm replacing# stdout_handler = logging.StreamHandler(stream=sys.stdout)stdout_handler.setFormatter(logging.Formatter(fmt='%(message)s'))
# stdout_handler.setLevel(logging.DEBUG)returnstdout_handler
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want all ERROR messages to the console to be in red. I'm not talking about the level text, but the actual message. I'm completely new to rich. Looking at the code for RichHandler, it seems like a custom Highlighter is the best approach. Is there a better method?
I used a filter to change the highlighter on a given LogRecord depending on its level.
This is related to #3156.
Beta Was this translation helpful? Give feedback.
All reactions