Python Progress Bar THROUGH Logging Module Python Progress Bar THROUGH Logging Module python python

Python Progress Bar THROUGH Logging Module


I couldn't find a good solution for this, so I wrote enlighten progress bar to handle it. Basically it changes the scroll area of the terminal so logging is done above the progress bar(s) rather than having to redraw the progress bar(s) every time you want to write to STDOUT. This lets you write to the terminal as much as you want without having to modify logging, print, etc.

import loggingimport timeimport enlighten# Setup logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger()# Setup progress barmanager = enlighten.get_manager()pbar = manager.counter(total=100, desc='Ticks', unit='ticks')for i in range(1, 101):    logger.info("Processing step %s" % i)    time.sleep(.2)    pbar.update()


I solved it like this:

import loggingimport timefrom tqdm import tqdmimport ioclass TqdmToLogger(io.StringIO):    """        Output stream for TQDM which will output to logger module instead of        the StdOut.    """    logger = None    level = None    buf = ''    def __init__(self,logger,level=None):        super(TqdmToLogger, self).__init__()        self.logger = logger        self.level = level or logging.INFO    def write(self,buf):        self.buf = buf.strip('\r\n\t ')    def flush(self):        self.logger.log(self.level, self.buf)if __name__ == "__main__":    logging.basicConfig(format='%(asctime)s [%(levelname)-8s] %(message)s')    logger = logging.getLogger()    logger.setLevel(logging.DEBUG)    tqdm_out = TqdmToLogger(logger,level=logging.INFO)    for x in tqdm(range(100),file=tqdm_out,mininterval=30,):        time.sleep(.5)

Output

2016-12-19 15:35:06 [INFO    ] 16%|#####9                                | 768/4928 [07:04<40:50,  1.70it/s]2016-12-19 15:36:07 [INFO    ] 18%|######6                               | 865/4928 [08:04<40:34,  1.67it/s]


You can use the tqdm progress bar with a custom handler through logging as described here:

import loggingimport timeimport colorlogfrom tqdm import tqdmclass TqdmHandler(logging.StreamHandler):    def __init__(self):        logging.StreamHandler.__init__(self)    def emit(self, record):        msg = self.format(record)        tqdm.write(msg)if __name__ == "__main__":    for x in tqdm(range(100)):        logger = colorlog.getLogger("MYAPP")        logger.setLevel(logging.DEBUG)        handler = TqdmHandler()        handler.setFormatter(colorlog.ColoredFormatter(            '%(log_color)s%(name)s | %(asctime)s | %(levelname)s | %(message)s',            datefmt='%Y-%d-%d %H:%M:%S',            log_colors={                'DEBUG': 'cyan',                'INFO': 'white',                'SUCCESS:': 'green',                'WARNING': 'yellow',                'ERROR': 'red',                'CRITICAL': 'red,bg_white'},))        logger.addHandler(handler)        logger.debug("Inside subtask: "+str(x))        time.sleep(.5)