-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
Only See the reference for details on highlighting. |
I hope we solved your problem. If you like using Rich, you might also enjoy Textual |
Thanks for your help, do you know if you can help with this issue regarding |
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? |
Yes, I would like to remove the |
You could implement a handler that strips console markup. |
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![/]") |
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 usemarkup=True
it results in the exported file as shown below:Q2:
Why is it that passing in
highlighter=None
when initializingRichHandler
in the first place doesn't take effect, but instead I need to pass inextra={'highlighter': None}
again inlog.info
, is this how it was originally designed to work?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"
The text was updated successfully, but these errors were encountered: