Open MatrixManAtYrService opened 5 months ago
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.
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.
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:So setting highlighter to
None
doesn't disable it. Eventually I settled on this:...which works just fine
This would be easier to work with if either the default value for the kwarg was
ReprHighlighter
, and setting it toNone
effectively disabled the highlighter. Or, if that's undesirable for some reason, if the use ofNullHighligher
withRichHandler
was documented in the "how to disable" section here: https://rich.readthedocs.io/en/latest/highlighting.htmlPlatform MacOS rich==13.7.1