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
logging_redirect_tqdm not respecting log level of original stream handler #1272
Comments
This implementation is intrusive. |
@phausamann I have also noticed this, however your example is incorrect as ...
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(logging.Formatter(fmt='{levelname:>8}: {message}', style='{')) # Formatter to see the difference
root_logger.addHandler(stream_handler) # This line is required to see the formatted output (actually use the stream_handler)
... I would also be happy if the missing line was added to resolve this issue. |
@alkatar21 you're right, my bad. Here's a modified example that also demonstrates that it works correctly without the context manager:
|
@phausamann happy to accept a PR :) |
Multiple PRs submitted, please review this issue! |
If it's really just a oneliner to fix this issue and it's still not fixed (after years of reported issues and multiple unreviewed PRs) then the point has come that I sadly have to ditch tqdm. It's not a minor issue, logging is really important, and tqdm messes it all up |
here's a wrapper workaround @contextlib.contextmanager
def logging_redirect_tqdm(
loggers: list[logging.Logger] | None=None,
tqdm_class: type[tqdm.std.tqdm]=tqdm.std.tqdm,
*,
level: int | None = None,
) -> Iterator[None]:
"""Replaces StreamHandlers with a TQDM handler of the same level."""
# Fixes https://github.com/tqdm/tqdm/issues/1272
loggers = loggers or [logging.getLogger()]
if level is None:
for logger in loggers:
for handler in logger.handlers:
if isinstance(handler, logging.StreamHandler):
level = handler.level
break
with tqdm.contrib.logging.logging_redirect_tqdm(loggers, tqdm_class):
if level is not None:
for logger in loggers:
for handler in logger.handlers:
if isinstance(handler, tqdm.contrib.logging._TqdmLoggingHandler):
handler.setLevel(level)
yield |
Any update for this yet? |
Description
I've run into an issue with
logging_redirect_tqdm
when the stream handler log level differs from the root logger level.In my use case, I want to log to a file at debug level and to the console at info level, which is why I need to set the root logger to debug level.
This causes an issue with
logging_redirect_tqdm
because it uses the root logger level for the redirected handler:Output:
Fix
Adding
after this line seems to fix the issue. I'd be happy to open a PR.
Versions
The text was updated successfully, but these errors were encountered: