logging: inherit output stream from existing handler#1191
Merged
casperdcl merged 1 commit intotqdm:develfrom Aug 14, 2021
Merged
logging: inherit output stream from existing handler#1191casperdcl merged 1 commit intotqdm:develfrom
casperdcl merged 1 commit intotqdm:develfrom
Conversation
When using the logging redirection, logs will currently always be printed
to stdout, while the logging module default is to print to stderr.
Fix this by trying to inherit the stream from the existing handler, like
the code already does for the formatter.
Consider the following example:
import logging
import time
from tqdm.contrib.logging import tqdm_logging_redirect
log = logging.getLogger()
log.warning("start")
with tqdm_logging_redirect(range(int(4))) as pbar:
for i in pbar:
time.sleep(0.1)
log.warning(f"Step {i}")
log.warning("done")
Running this while redirecting stdout (`$ python3 log.py > /dev/null`)
without this patch will print:
$ venv/bin/python log.py > /dev/null
start
100%|████████████████████████████████████████████| 4/4 [00:00<00:00, 9.87it/s]
done
After this patch:
$ venv/bin/python log.py > /dev/null
start
Step 0
Step 1
Step 2
Step 3
100%|████████████████████████████████████████████| 4/4 [00:00<00:00, 9.83it/s]
done
Signed-off-by: Steffan Karger <steffan.karger@fox-it.com>
casperdcl
approved these changes
Jun 21, 2021
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using the logging redirection, logs will currently always be printed
to stdout, while the logging module default is to print to stderr.
Fix this by trying to inherit the stream from the existing handler, like
the code already does for the formatter.
Consider the following example:
Running this while redirecting stdout (
$ python3 log.py > /dev/null)without this patch will print:
After this patch:
Signed-off-by: Steffan Karger steffan.karger@fox-it.com