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>
added logging sub-module
python 2 compatibility
fixed python 2 fix
added test for custom tqdm class
python 2 absolute imports
(due to otherwise conflicting `logging` module)
isort
more tests relating to _get_first_found_console_formatter
isort
minor simplification
test handleError
test logging formatter being used
minor rename to _get_first_found_console_logging_formatter
test that certain exceptions are not swallowed
avoid using mock.assert_called (py 3.5)
moved to tqdm.contrib.logging
added "Redirecting console logging to tqdm" readme
removed no longer necessary absolute_import declaration
minor: updated package of example in docstring
Only call `tqdm.write` (which inserts a newline) if
`DummyTqdmFile.write` is itself called with a string containing a
newline; otherwise buffer the rest of the string.
See changes in test_tqdm.py for the patterns this allows.