Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug? Docs?] It's not obvious from the docs how to disable highlighting when using RichHandler #3376

Open
1 of 2 tasks
MatrixManAtYrService opened this issue Jun 8, 2024 · 3 comments

Comments

@MatrixManAtYrService
Copy link

  • 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.
Screenshot from 2024-06-08 15-03-53

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

Copy link

github-actions bot commented Jun 8, 2024

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

@Jannchie
Copy link

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)

@latent-capital
Copy link

latent-capital commented Sep 19, 2024

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.

image

Conclusion: The rich lifestyle is a privilege that you have you earn again every day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants