I can't find enough documentation to get this to work. I have non-longrunning processes that show progress information on stderr and use stdout for output. What I'd like is the log messages to appear all collated at the end when the processes exit, plus it should log to a file as well. From what I can find in the documentation, MemoryHandler and FileHandler are what I need. But when I set this up like below, I don't get any output, neither in the file nor on exit. Any help would be appreciated.
import logging, logging.handlers, atexit, sys
filename = 'mylogfile.txt'
logLevel = logging.DEBUG
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
streamhandler = logging.StreamHandler(sys.stderr)
streamhandler.setLevel(logLevel)
streamhandler.setFormatter(formatter)
memoryhandler = logging.handlers.MemoryHandler(1024*100, logLevel, streamhandler)
filehandler = logging.FileHandler(filename)
filehandler.setLevel(logLevel)
filehandler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(memoryhandler)
logger.addHandler(filehandler)
def flush():
memoryhandler.flush()
atexit.register(flush)
logger.debug("Logger has Initialized")