[Bug? Docs?] It's not obvious from the docs how to disable highlighting when using `RichHandler`
- [x] I've checked docs and closed issues for possible solutions.
- [ ] I can't find my issue in the FAQ. (I found it, but there's more to it)
Describe the bug
I noticed unexpected splotches of green in my log ouput using RichHandler.
I see that the FAQ says I need to disable highlighting. The initializer for RichHandler does this, however:
HIGHLIGHTER_CLASS: ClassVar[Type[Highlighter]] = ReprHighlighter
...
self.highlighter = highlighter or self.HIGHLIGHTER_CLASS()
So setting highlighter to None doesn't disable it. Eventually I settled on this:
RichHandler(highlighter=NullHighlighter())
...which works just fine
This would be easier to work with if either the default value for the kwarg was ReprHighlighter, and setting it to None effectively disabled the highlighter. Or, if that's undesirable for some reason, if the use of NullHighligher with RichHandler was documented in the "how to disable" section here: https://rich.readthedocs.io/en/latest/highlighting.html
Platform MacOS rich==13.7.1
Thank you for your issue. Give us a little time to review it.
PS. You might want to check the FAQ if you haven't done so already.
This is an automated reply, generated by FAQtory
I also found this very confusing. Initially, I tried to provide a console without highlighter, but none of them worked:
console = Console(highlighter=None) # not work
console = Console(highlighter=NullHighlighter()) # not work
RichHandler(console=console)
I also found this very confusing. Initially, I tried to provide a console without highlighter, but none of them worked:
console = Console(highlighter=None) # not work console = Console(highlighter=NullHighlighter()) # not work RichHandler(console=console)
By carefully analyzing your statement, I managed to track down the issue in detail.
- Tested rich versions: 13.7.1 and 13.8.1.
All relevant behavior is computed in the following snippet.
import logging
from rich.console import Console
from rich.logging import RichHandler
from rich.highlighter import NullHighlighter
console = Console(
log_path=False,
highlight=False,
#highlighter=None, # works outside the logging facility
#highlighter=NullHighlighter(), # works outside the logging facility
)
# Define logger before use
logger = logging.getLogger()
# Clear handlers from previous runs
logger.handlers = []
# Setup logging with RichHandler
logging.basicConfig(
level="INFO",
format="%(message)s",
handlers=[
RichHandler(
console=console,
show_path=True,
rich_tracebacks=True,
markup=True,
show_time=False,
# uncommenting this line will force highlighting off correctly
#highlighter=NullHighlighter(),
# to some peoples' surprise, this line will NOT force highlighting off
#highlighter=None,
)
],
)
# Expected result: this block should NOT be highlighted
# - highlighting is forced AFTER markup is applied ()
logger.info("[bold red] init() null[/bold red]")
# this diff output is now partly black on black due to rich highlighting
logger.info("- <script>alert('test');</script>")
# this works in compliance with the docs
# https://rich.readthedocs.io/en/stable/logging.html?highlight=extra
logger.info("- <script>alert('test');</script>", extra={"highlighter": None} )
logger.info("- <script>alert('test');</script>", extra={"highlighter": NullHighlighter()} )
# switching outside the logging facility works
console.print("get-1337", highlight=True)
console.print("be-1337", highlight=False)
console.log( "get-1337", highlight=True )
console.log( "be-1337", highlight=False)
Result: Above users find it inconvenient, that the first two lines of the following output are rendered with highlighting - line 8 clearly states "highlight=False", line 9 and line 10 have no effect on the logging facility when uncommented.
Solution in the snippet: Pass extra={"highlighter": None} or extra={"highlighter": NullHighlighter()} to the logging facility, as shown in line 45 / 46.
Conclusion: The rich lifestyle is a privilege that you have you earn again every day.