title: How to Redirect Logger Output into PyQt Text Widget
body: <p>A code posted on <a href="https://stackoverflow.com/questions/11465971/redirecting-output-in-pyqt">Redirecting Output in PyQt</a> does two good things at once: it takes advantage of <code>logging</code> module to nicely format messages and it redirects standard <code>stdout</code> and <code>stderr</code> in to QT <code>QTextBrowser</code> widget. 
  But I would like <code>QTextBrowser</code> to receive all the print output coming out of running code. Particularly I want to redirect the nicely formatted messages that come from logger.
  An ideal solution would re-direct every logger. output in to <code>QTextBrowser</code> (and not just <code>stdout</code> and <code>stderr</code>). As a matter of fact I would rather redirect logger's messages instead of <code>stdout</code> and <code>stderr</code> ones if I would have to make a choice between the twos....
  So here are the commands used to printout formatted messages:</p>
  
  <pre><code>logger.debug('debug message')
  logger.info('info message')
  logger.warning('warning message')
  logger.error('error message')
  </code></pre>
  
  <p>And here is the code:
  <img src="https://i.stack.imgur.com/6iFND.jpg" alt="enter image description here"></p>
  
  <p>========</p>
  
  <pre><code>import sys
  from PyQt4 import QtCore, QtGui
  import logging
  logger = logging.getLogger(__name__)
  
  class XStream(QtCore.QObject):
      _stdout = None
      _stderr = None
      messageWritten = QtCore.pyqtSignal(str)
      def flush( self ):
          pass
      def fileno( self ):
          return -1
      def write( self, msg ):
          if ( not self.signalsBlocked() ):
              self.messageWritten.emit(unicode(msg))
  
      @staticmethod
      def stdout():
          if ( not XStream._stdout ):
              XStream._stdout = XStream()
              sys.stdout = XStream._stdout
          return XStream._stdout
  
      @staticmethod
      def stderr():
          if ( not XStream._stderr ):
              XStream._stderr = XStream()
              sys.stderr = XStream._stderr
          return XStream._stderr
  
  class MyDialog(QtGui.QDialog):
      def __init__( self, parent = None ):
          super(MyDialog, self).__init__(parent)
  
          self._console = QtGui.QTextBrowser(self)
          self._button  = QtGui.QPushButton(self)
          self._button.setText('Test Me')
  
          layout = QtGui.QVBoxLayout()
          layout.addWidget(self._console)
          layout.addWidget(self._button)
          self.setLayout(layout)
  
          XStream.stdout().messageWritten.connect( self._console.insertPlainText )
          XStream.stderr().messageWritten.connect( self._console.insertPlainText )
  
          self._button.clicked.connect(self.test)
  
      def test( self ):
          print 'printing LINE 1'
          print 'printing LINE 2'
          logger.debug('debug message')
          logger.info('info message')
          logger.warning('warning message')
          logger.error('error message')
          # error out something
          print blah
  
  if ( __name__ == '__main__' ):
      # logging.basicConfig()
      # logging.basicConfig(filename='example.log',level=logging.DEBUG)
      logging.basicConfig(level=logging.DEBUG)
  
      app = None
      if ( not QtGui.QApplication.instance() ):
          app = QtGui.QApplication([])
  
      dlg = MyDialog()
      dlg.show()
  
      if ( app ):
          app.exec_()
  </code></pre>
  
  <h1>POSTED LATER::FULLY WORKING EXAMPLE::SOLVED BY Mr.Dano</h1>
  
  <pre><code>import sys
  from PyQt4 import QtCore, QtGui
  import logging
  
  class QtHandler(logging.Handler):
      def __init__(self):
          logging.Handler.__init__(self)
      def emit(self, record):
          record = self.format(record)
          if record: XStream.stdout().write('%s\n'%record)
          # originally: XStream.stdout().write("{}\n".format(record))
  
  
  logger = logging.getLogger(__name__)
  handler = QtHandler()
  handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
  logger.addHandler(handler)
  logger.setLevel(logging.DEBUG)
  
  
  class XStream(QtCore.QObject):
      _stdout = None
      _stderr = None
      messageWritten = QtCore.pyqtSignal(str)
      def flush( self ):
          pass
      def fileno( self ):
          return -1
      def write( self, msg ):
          if ( not self.signalsBlocked() ):
              self.messageWritten.emit(unicode(msg))
      @staticmethod
      def stdout():
          if ( not XStream._stdout ):
              XStream._stdout = XStream()
              sys.stdout = XStream._stdout
          return XStream._stdout
      @staticmethod
      def stderr():
          if ( not XStream._stderr ):
              XStream._stderr = XStream()
              sys.stderr = XStream._stderr
          return XStream._stderr
  
  class MyDialog(QtGui.QDialog):
      def __init__( self, parent = None ):
          super(MyDialog, self).__init__(parent)
  
          self._console = QtGui.QTextBrowser(self)
          self._button  = QtGui.QPushButton(self)
          self._button.setText('Test Me')
  
          layout = QtGui.QVBoxLayout()
          layout.addWidget(self._console)
          layout.addWidget(self._button)
          self.setLayout(layout)
  
          XStream.stdout().messageWritten.connect( self._console.insertPlainText )
          XStream.stderr().messageWritten.connect( self._console.insertPlainText )
  
          self._button.clicked.connect(self.test)
  
      def test( self ):
          logger.debug('debug message')
          logger.info('info message')
          logger.warning('warning message')
          logger.error('error message')
          print 'Old school hand made print message'
  
  if ( __name__ == '__main__' ):
      app = None
      if ( not QtGui.QApplication.instance() ):
          app = QtGui.QApplication([])
      dlg = MyDialog()
      dlg.show()
      if ( app ):
          app.exec_()
  </code></pre>