logging_redirect_tqdm bug when no console logging handler is present
schoennenbeck opened this issue · 0 comments
- I have marked all applicable categories:
- exception-raising bug
- visual output bug
- I have visited the source website, and in particular
read the known issues - I have searched through the issue tracker for duplicates
- I have mentioned version numbers, operating system and
environment, where applicable:import tqdm, sys print(tqdm.__version__, sys.version, sys.platform) # 4.66.1 3.10.12 (main, Jul 5 2023, 18:54:27) [GCC 11.2.0] linux
logging_redirect_tqdm
is supposed to redirect the logging output of all console handlers of all given loggers to tqdm.write
. It does that by (temporarily) removing all console handlers from the given loggers and adding a new tqdm-handler (which inherits its formatter from the first found console logging handler). However, if a logger originally had no console logging handlers the tqdm handler is still added which means that the logger now starts to log to the console where it previously did not.
From my point of view this is a bug, since it does not redirect console output to tqdm.write
but rather introduces new output that was not there previously (also this behaviour is not stated in the docstring).
The relevant code in tqdm/tqdm/contrib/logging.py
currently reads:
for logger in loggers:
tqdm_handler = _TqdmLoggingHandler(tqdm_class)
orig_handler = _get_first_found_console_logging_handler(logger.handlers)
if orig_handler is not None:
tqdm_handler.setFormatter(orig_handler.formatter)
tqdm_handler.stream = orig_handler.stream
logger.handlers = [
handler for handler in logger.handlers
if not _is_console_logging_handler(handler)] + [tqdm_handler]
Simply adjusting the indent-level of logger.handlers = [...]
to fall under the if-block should take care of this issue (we only want the new handler if there was at least one console handler to begin with).