title: Storing logger messages in a string
body: <p>I wanted to store all the intermediate log messages (warn, info, error) to a string in Python, and report those log messages to the console at the end of program.</p>
  <p>I tried to follow the steps outlined in
  <a href="http://opensourcehacker.com/2011/02/23/temporarily-capturing-python-logging-output-to-a-string-buffer/" rel="noreferrer">http://opensourcehacker.com/2011/02/23/temporarily-capturing-python-logging-output-to-a-string-buffer/</a>
  but was unsuccessful .</p>
  <p>Could somebody tell me a short, clean way to do this?</p>
  <p>This is what I've tried for now:</p>
  <pre><code>log = logging.getLogger('basic_logger')
  log.setLevel(logging.DEBUG)
  report = &quot;&quot;
  
  memory_handler = logging.handlers.MemoryHandler(1024*20, logging.ERROR, report)
  memory_handler.setLevel(logging.DEBUG)
  log.addHandler(memory_handler)
      
  log.info(&quot;hello world&quot;)
      
  memory_handler.flush()
      
  print &quot;report:&quot;, report
  </code></pre>