Skip to content

tqdm.contrib.logging#

Helper functionality for interoperability with stdlib logging.

logging_redirect_tqdm#

[view source]

@contextmanager
def logging_redirect_tqdm(loggers=None, tqdm_class=std_tqdm)

Context manager redirecting console logging to tqdm.write(), leaving other logging handlers (e.g. log files) unaffected.

Parameters

  • loggers: list, optional
    Which handlers to redirect (default: [logging.root]).
  • tqdm_class: optional

Example

import logging
from tqdm import trange
from tqdm.contrib.logging import logging_redirect_tqdm

LOG = logging.getLogger(__name__)

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    with logging_redirect_tqdm():
        for i in trange(9):
            if i == 4:
                LOG.info("console logging redirected to `tqdm.write()`")
    # logging restored

tqdm_logging_redirect#

[view source]

@contextmanager
def tqdm_logging_redirect(*args, **kwargs)

Convenience shortcut for:

with tqdm_class(*args, **tqdm_kwargs) as pbar:
    with logging_redirect_tqdm(loggers=loggers, tqdm_class=tqdm_class):
        yield pbar

Parameters

  • tqdm_class: optional, (default: tqdm.std.tqdm).
  • loggers: optional, list.
    **tqdm_kwargs : passed to tqdm_class.