Skip to content

Log to stderr without breaking progress bars #1317

Answered by ewels
ewels asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, had a "duh" moment this morning when it clicked that this is because the log messages and progress bar are using different consoles.

If we create a Console instance and then use it for both the log messages and the progress bar, it works fine:

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

myconsole = Console(stderr=True)
logging.basicConfig(level="NOTSET", handlers=[RichHandler(console=myconsole)])
log = logging.getLogger()

for n in track(range(5), description="Processing...", console=myconsole):
    log.info(f"Working.. {n}")
    time.sleep(0.2)

Solved! Thanks for the rubber-duck treatment! 🦆 😀

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@willmcgugan
Comment options

Answer selected by ewels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants