title: How to redirect stderr in Python?
body: <p>I would like to log all the output of a Python script. I tried:</p>
  
  <pre><code>import sys
  
  log = []
  
  class writer(object):
      def write(self, data):
          log.append(data)
  
  sys.stdout = writer()
  sys.stderr = writer()
  </code></pre>
  
  <p>Now, if I "print 'something' " it gets logged. But if I make for instance some syntax error, say "print 'something# ", it wont get logged - it will go into the console instead.</p>
  
  <p>How do I capture also the errors from Python interpreter?</p>
  
  <p>I saw a possible solution here:</p>
  
  <p><a href="http://www.velocityreviews.com/forums/showpost.php?p=1868822&amp;postcount=3" rel="noreferrer">http://www.velocityreviews.com/forums/showpost.php?p=1868822&amp;postcount=3</a></p>
  
  <p>but the second example logs into /dev/null - this is not what I want. I would like to log it into a list like my example above or StringIO or such...</p>
  
  <p>Also, preferably I don't want to create a subprocess (and read its stdout and stderr in separate thread).</p>