title: How to gather and sort logging output from multiprocesses
body: <p>I have a CLI tool written in Python that operates on a bunch of files and makes use of processes for small file operations. While the parts executed as threads are only subsections in a longer file flow, the logging output of the threaded actions is (obviously) asynchronous.</p>
  
  <p>My idea would be to buffer all logging output in memory (it is very limited output) until the processes have finished, and then sort all logging by process ID before actually outputting it to STDERR. I understand most parts of the logging tutorial, but this is a bit over my head. I attempted using the MemoryHandler, but couldn't make much sense of how to implement custom flush conditions or why it needs a target Handler itself.</p>
  
  <p>This code illustrates my basic problem:</p>
  
  <pre><code>logger = logging.getLogger()
  logger.handlers = []
  logging.basicConfig(format='%(levelname)s %(process)s: %(message)s',
                      level=logging.DEBUG)
  
  def work():
      for i in range(5):
          logging.debug(str(i))
          time.sleep(random())
  
  if __name__ == "__main__":
      processes = []
      for i in range(3):
          p = Process(target=work)
          processes.append(p)
          p.start()
  
      for p in processes:
          p.join()
  </code></pre>
  
  <p>The <em>synchronous</em> output is something like:</p>
  
  <pre><code>DEBUG 64571: 0
  DEBUG 64572: 0
  DEBUG 64573: 0
  DEBUG 64572: 1
  DEBUG 64572: 2
  DEBUG 64572: 3
  DEBUG 64572: 4
  DEBUG 64573: 1
  DEBUG 64571: 1
  DEBUG 64573: 2
  DEBUG 64573: 3
  DEBUG 64573: 4
  DEBUG 64571: 2
  DEBUG 64571: 3
  DEBUG 64571: 4
  </code></pre>
  
  <p>Instead of this output, I would like the console output to be halted while the process runs, and then output the logs sorted by process ID, ideally outputting as soon as the next-first process has finished.</p>
  
  <p>I'm mostly looking for what to conceptually do and what modules and methods could be useful for this. With my code my main questions are:</p>
  
  <p>a) How do I us the MemoryHandler to store the output and how do I format it once a process is finished? Or is that not a viable idea altogether?
  b) How can I monitor processes finishing, either one by one, or all (I guess the later is just code after the p.join() loop)?
  c) Outside my isolated example, how to turn this "process-sorted" logging on before, and off after, the section in my code that uses multithreading.</p>
  
  <p>Thanks for any pointers ;)</p>