Guillochon Guillochon - 4 months ago 40
Python Question

Change logging "print" function to "tqdm.write" so logging doesn't interfere with progress bars

I have a simple question: How do I change the built-in Python logger's

print
function to
tqdm.write
such that logging messages do not interfere with tqdm's progress bars? Thanks!

Answer

You need a custom logging handler:

import logging
import tqdm

class TqdmLoggingHandler (logging.Handler):
    def __init__ (self, level = logging.NOTSET):
        super (self.__class__, self).__init__ (level)

    def emit (self, record):
        try:
            msg = self.format (record)
            tqdm.tqdm.write (msg)
            self.flush ()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)          

and then add this to the logging chain:

import time

log = logging.getLogger (__name__)
log.setLevel (logging.INFO)
log.addHandler (TqdmLoggingHandler ())
for i in tqdm.tqdm (range (100)):
    if i == 50:
        log.info ("Half-way there!")
    time.sleep (0.1)