The Easiest Way to Save and Share Code Snippets on the web

Overwriting twistd's output formatting

python

last edit: Feb, 7th 2012 | jump to bottom

import sys
from twisted.python import log, util
from twisted.internet import reactor
from twisted.application.service import Application
 
class MyLogObserver(log.FileLogObserver):
  """Custom Logging observer"""
  def emit(self,eventDict):
    """Custom emit for FileLogObserver"""
    text = log.textFromEventDict(eventDict)
    if text is None:
      return
    self.timeFormat='[%Y-%m-%d %H:%M:%S]'
    timeStr = self.formatTime(eventDict['time'])
    fmtDict = {'text': text.replace("\n", "\n\t")}
    msgStr = log._safeFormat("%(text)s\n", fmtDict)
    util.untilConcludes(self.write, timeStr + " " + msgStr)
    util.untilConcludes(self.flush)
 
application=Application("fileget")
application.setComponent(log.ILogObserver, MyLogObserver(sys.stdout).emit)
reactor.callWhenRunning(log.msg,"Better way of adjusting format!")
56 views