pythonloggingescapingrich

Why is Python Rich printing in green?


Using Rich I get what I think is a spurious green output in the console.

In the following code, the "d:c" is coloured green, the rest of the text is as expected.

from rich.logging import RichHandler
import logging
    
logging.basicConfig(    
    handlers=[RichHandler()],
    format='%(message)s'
)

logging.getLogger().setLevel(logging.DEBUG)
    
logging.info("Why is this green d:c?")

Logging output showing unexpected green foreground for just the letters d:c

I've read the rich documentation and I've tried:

RichHandler(markup=False)
logging.info(escape("Why is this green d:c?"))

It's the same in Jupyter, Windows (11) and Linux (Ubuntu 24.04)

I'm using Rich version 13.9.3.

Is this expected, and what do I need to escape or disable to stop the green output?


Solution

  • Rich does some automatic highlighting by default (specifically, RichHandler defaults to using ReprHighlighter, which "Highlights the text typically produced from __repr__ methods."). Either do logging.info("Why is this green d:c?", extra={"highlighter": None}) to turn it off for an individual message, or from rich.highlighter import NullHighlighter and handlers=[RichHandler(highlighter=NullHighlighter())], to turn it off for all messages. Doing markup=False and escape didn't work because those only affect markup in the message itself, not automatic highlighting.