Skip to content

[BUG]Passing Console() to RichHandler() interrupts Progress/track when logging #3769

Closed
@marcelzwiers

Description

@marcelzwiers

Describe the bug
I cannot get RichHandler to work when passing a console, in conjunction with Progress()/track(). I would also be happy if I could pass theme=Theme(..) to RichHandler().

See also:
#1173
#3238

Platform
Konsole terminal on OpenSuse Linux, Python = 3.13

Click to expand
$ python -m rich.diagnose
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=171 ColorSystem.TRUECOLOR>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = 'truecolor'                                                   │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 61                                                            │
│    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=171, height=61),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=171,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=61,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=171, height=61)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 171                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-256color',      │
│     'COLORTERM': 'truecolor',      │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"
(bidscoin_3.13) ~/PycharmProjects/bidscoin$ pip freeze | grep rich
rich @ file:///croot/rich_1732638981168/work

Code example

import logging
import time
from rich.logging import RichHandler
from rich.progress import track
from rich.console import Console

logger = logging.getLogger(__name__)
logger.setLevel('DEBUG')


# Test the RichHandler with Console() 
consolehandler = RichHandler(console=Console())
logger.addHandler(consolehandler)
for n in track(range(5), description="Doing work using Console()"):
    logger.info(f"Logging {n} in the middle of progress...")
    time.sleep(0.2)

# Test the RichHandler without Console() 
logger.removeHandler(consolehandler)
consolehandler = RichHandler()
logger.addHandler(consolehandler)
for n in track(range(5), description="Doing work without using Console()"):
    logger.info(f"Logging {n} in the middle of progress...")
    time.sleep(0.2)

Output

Doing work using Console() ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━   0% -:--:--[06/18/25 01:19:20] INFO     Logging 0 in the middle of progress...                                                                                      test_logging.py:18
Doing work using Console() ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━   0% -:--:--[06/18/25 01:19:21] INFO     Logging 1 in the middle of progress...                                                                                      test_logging.py:18
Doing work using Console() ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━   0% -:--:--                    INFO     Logging 2 in the middle of progress...                                                                                      test_logging.py:18
Doing work using Console() ━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━━━━━━  40% 0:00:01                    INFO     Logging 3 in the middle of progress...                                                                                      test_logging.py:18
Doing work using Console() ━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━  60% 0:00:01                    INFO     Logging 4 in the middle of progress...                                                                                      test_logging.py:18
Doing work using Console() ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:01
[06/18/25 01:19:21] INFO     Logging 0 in the middle of progress...                                                                                      test_logging.py:27
[06/18/25 01:19:22] INFO     Logging 1 in the middle of progress...                                                                                      test_logging.py:27
                    INFO     Logging 2 in the middle of progress...                                                                                      test_logging.py:27
                    INFO     Logging 3 in the middle of progress...                                                                                      test_logging.py:27
                    INFO     Logging 4 in the middle of progress...                                                                                      test_logging.py:27
Doing work without using Console() ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:01

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions