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] About FileHandler #3244

Closed
Byxs20 opened this issue Jan 1, 2024 · 8 comments
Closed

[BUG] About FileHandler #3244

Byxs20 opened this issue Jan 1, 2024 · 8 comments

Comments

@Byxs20
Copy link

Byxs20 commented Jan 1, 2024

Describe the bug

python version:3.8.10
rich version: 13.7.0

Q1:
Hi, I am trying to export a log file using logging.FileHandler, but I found that if I use markup=True it results in the exported file as shown below:

import logging
from rich.logging import RichHandler

logging.basicConfig(
    level=logging.DEBUG,
    format="%(message)s",
    datefmt="[%X]",
    handlers=[
        RichHandler(markup=True),
        logging.FileHandler("debug.txt", mode='w')
    ]
)

log = logging.getLogger()
log.info("[red] Hello World![/]")

image

Q2:

import logging
from rich.logging import RichHandler

logging.basicConfig(
    level=logging.DEBUG,
    format="%(message)s",
    datefmt="[%X]",
    handlers=[
        RichHandler(markup=True, highlighter=None),
    ]
)

log = logging.getLogger()
log.info("[green] 1d0f936a-100e-4003-9f6d-d2298d2c8cc0 [/]")

Why is it that passing in highlighter=None when initializing RichHandler in the first place doesn't take effect, but instead I need to pass in extra={'highlighter': None} again in log.info, is this how it was originally designed to work?

image

image

Platform

Click to expand

╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface. │
│ │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│ │
│ color_system = 'truecolor' │
│ encoding = 'utf-8' │
│ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │
│ height = 22 │
│ is_alt_screen = False │
│ is_dumb_terminal = False │
│ is_interactive = True │
│ is_jupyter = False │
│ is_terminal = True │
│ legacy_windows = False │
│ no_color = False │
│ options = ConsoleOptions( │
│ size=ConsoleDimensions(width=165, height=22), │
│ legacy_windows=False, │
│ min_width=1, │
│ max_width=165, │
│ is_terminal=True, │
│ encoding='utf-8', │
│ max_height=22, │
│ justify=None, │
│ overflow=None, │
│ no_wrap=False, │
│ highlight=None, │
│ markup=None, │
│ height=None │
│ ) │
│ quiet = False │
│ record = False │
│ safe_box = True │
│ size = ConsoleDimensions(width=165, height=22) │
│ soft_wrap = False │
│ stderr = False │
│ style = None │
│ tab_size = 8 │
│ width = 165 │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭── <class 'rich._windows.WindowsConsoleFeatures'> ───╮
│ Windows features available. │
│ │
│ ╭─────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=True, truecolor=True) │ │
│ ╰─────────────────────────────────────────────────╯ │
│ │
│ truecolor = True │
│ vt = True │
╰─────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ { │
│ 'TERM': None, │
│ 'COLORTERM': 'truecolor', │
│ 'CLICOLOR': None, │
│ 'NO_COLOR': None, │
│ 'TERM_PROGRAM': 'vscode', │
│ 'COLUMNS': None, │
│ 'LINES': None, │
│ 'JUPYTER_COLUMNS': None, │
│ 'JUPYTER_LINES': None, │
│ 'JPY_PARENT_PID': None, │
│ 'VSCODE_VERBOSE_LOGGING': None │
│ } │
╰────────────────────────────────────╯
platform="Windows"

@Byxs20 Byxs20 changed the title [BUG] [BUG] About FileHandler Jan 1, 2024
Copy link

github-actions bot commented Jan 1, 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

@willmcgugan
Copy link
Collaborator

Only RichHandler is aware of console markup.

See the reference for details on highlighting.

Copy link

github-actions bot commented Jan 1, 2024

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

@Byxs20
Copy link
Author

Byxs20 commented Jan 1, 2024

Thanks for your help, do you know if you can help with this issue regarding logging.FileHandler or have a better example to correct it?

@willmcgugan
Copy link
Collaborator

The logging module will simply pass the string to each handler. Its not clear what the issue is from your explanation. Do you want the markup stripped from the log file?

@Byxs20
Copy link
Author

Byxs20 commented Jan 1, 2024

Yes, I would like to remove the tag about the color, would make me very uncomfortable to view the log file in debug. But I need to keep the color of Console, and of course I'd prefer to work around this somehow, even if I can get the tag about the color removed from what's passed into logging.FileHandler, and I don't want to make it so that I don't have to use logging to get to the output of the code.

@willmcgugan
Copy link
Collaborator

You could implement a handler that strips console markup. Text.from_markup(log_message).plain will remove markup from log_message

@Byxs20
Copy link
Author

Byxs20 commented Jan 1, 2024

Thanks for your help, I have solved the problem using the following code.

import logging
from rich.logging import RichHandler
from rich.text import Text

class CustomFileHandler(logging.FileHandler):
    def emit(self, record):
        log_message = self.format(record)
        log_message_plain = remove_color_tags(log_message)
        self.stream.write(log_message_plain + self.terminator)

def remove_color_tags(log_message):
    text = Text.from_markup(log_message)
    return text.plain

logging.basicConfig(
    level=logging.DEBUG,
    format="%(message)s",
    datefmt="[%X]",
    handlers=[
        RichHandler(markup=True),
        CustomFileHandler("debug.txt", mode='w')
    ]
)

log = logging.getLogger()
log.info("[red]Hello World![/]")

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

2 participants