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] LoggingHandler doesn't write into the file but always inline in VSCode notebooks #3496

Open
jordi-torrents opened this issue Sep 18, 2024 · 2 comments

Comments

@jordi-torrents
Copy link

Rich's logging handler produces an inline logging line in VSCode notebooks when the logging should be redirected to a file, and this file never gets writen.

import logging
from rich.logging import RichHandler
from rich.console import Console
from pathlib import Path

logging.basicConfig(
    level=logging.DEBUG,
    handlers=[
        RichHandler(console=Console()),  # inline display
        RichHandler(  # writing log into test.log
            console=Console(file=Path("test.log").open("w", encoding="utf_8"))
        ),
    ],
)
logging.info("Hello world")

This code produces a duplicate logging and the file "test.log" remins empty.
image

Everything works fine when running from terminal.

Platform

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

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

@je-cook
Copy link

je-cook commented Feb 10, 2025

I ran into the same problem in a plain notebook (well, jupyter lab). As a work around if you set the following in Console it works as expected:

Console(file=Path("test.log").open("w", encoding="utf_8"),
               force_jupyter=False,
               force_terminal=False,  # this is needed because jupyter sets 'force_color' env var
)

The bug is that the jupyter notebook detection has a higher priority than a specified file.
There needs to be something to catch it in these locations but I'm not certain on the best implementation:

rich/rich/console.py

Lines 669 to 670 in 43d3b04

self.is_jupyter = _is_jupyter() if force_jupyter is None else force_jupyter
if self.is_jupyter:

if self.is_jupyter: # pragma: no cover

rich/rich/console.py

Lines 947 to 952 in 43d3b04

if self.is_jupyter:
# return False for Jupyter, which may have FORCE_COLOR set
return False
# If FORCE_COLOR env var has any value at all, we assume a terminal.
force_color = self._environ.get("FORCE_COLOR")

The first and second snippet is avoided by force_jupyter=False but this leads to the file having ANSI code sequences in the output which is off by default therefore force_terminal=False avoids the detection issue in the third snippet. If I come up with a nice solution i'll open a PR.

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