Python: Have an iterator iterate over log messages on a different thread
Question: I have 2 threads running:

    
    
    # Thread 1: In model
    logging.getLogger('ui').info("Sit still, I'm computing...")
    more_stuff = compute_stuff(stuff)
    logging.info("Ok, I'm done.")
    
    ...
    
    # Thread 2: In view, streaming messages to client
    for message in log_iterator('ui'):
        send_to_client(message)
    

I want log_iterator to hang until there's a log message, and then when one
comes up, do something with it. The question is, how do you make log_iterator?
Or is there a better way to achieve this goal?


Answer: You can write logs to file by 1 thread. Second thread can read the file and
shows logs.

This is logging configuration:

    
    
    import logging
    logger = logging.getLogger('simple_example') # set logger
    logger.setLevel(logging.INFO) # set logger level
    
    fh = logging.FileHandler('path-to-log-file') #set Handler
    fh.setLevel(logging.INFO) #set Handler level
    
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # set log format
    fh.setFormatter(formatter) # add format to Handler
    
    logger.addHandler(fh) # add Handler to logger
    

In my example function _rewrite_ logs to another file:

    
    
    def print_message():
        for i in range(0,1000):     
            f = open('path-to-log-file', 'r')
            s = f.read()
            f.close()
            f2 = open('path-to-log-file2', 'r+')
            f2.write(s)
            f2.close()
            time.sleep(.2)
    
    import threading
    t = threading.Thread(target=print_message)
    t.start()
    logger.error('error message1')
    logger.error('error message2')
    logger.error('error message3')
    t.join()
    

So logger write logs to file by first thread and second thread reads logs from
file. I think that this is what you want.

Also if you want to avoid writing logs to file you can make own Handler which
send log exactly to second thread. I think that it could be complicated but I
poorly know threading.

Here is logging documentation: <https://docs.python.org/2/howto/logging.html>

