[Twisted-Python] Help with porting .tac-based custom logging to twisted plugin architecture

general at vultaire.net general at vultaire.net
Tue Oct 20 23:59:45 MDT 2009


I have an app I'm working on, and until this point it's been done
utilizing a .tac file.  Recent changes in requirements are encouraging a
change to use the Twisted plugin system instead.  This seems clear for
the most part, but I'm unsure about the logging.

Specifically, in the .tac version, I'm using this:

    lf = logfile.DailyLogFile("my.log", ".")
    logname = "testlog"
    configure_python_logging(lf, logname)
    application.setComponent(log.ILogObserver,
                             log.PythonLoggingObserver(logname).emit)

However, in the plugin version, I'm not sure how to proceed since I
don't have an application object.  I can't seem to find any
documentation for configuring logging with plugins, either.  Is there a
way to make this work?

Just in case, I've pasted a complete working example of the .tac code
below, along with the partially complete plugin module.

- Paul Goins

####################################

###########################
# log_example/logtest.tac #
###########################
# -*- coding: utf-8 -*-

from twisted.application import service
from twisted.internet import reactor
from twisted.web import server as webserver

from logtest import log

application = service.Application("myapp")
log.init_logger_8dot2(application)

service = log.MyService()
service.setServiceParent(application)

###################################
# log_example/logtest/__init__.py #
###################################

##############################
# log_example/logtest/log.py #
##############################

# -*- coding: utf-8 -*-

from twisted.application.service import Service
from twisted.internet import reactor
from twisted.python import log, logfile
import logging

def init_logger_8dot2(application):
    lf = logfile.DailyLogFile("my.log", ".")
    logname = "testlog"
    configure_python_logging(lf, logname)
    application.setComponent(log.ILogObserver,
                             log.PythonLoggingObserver(logname).emit)

def configure_python_logging(file_obj, log_name):
    logging.basicConfig(stream=file_obj,
                        format="[%(asctime)s] %(levelname)s:
%(message)s",
                        datefmt="%Y-%m-%d %H:%M:%S")
    logger = logging.getLogger(log_name)
    logger.setLevel(logging.INFO)


class MyService(Service):

    def startService(self):
        reactor.callLater(2, reactor.stop)

#############################################
# log_example/twisted/plugins/log_plugin.py #
#############################################

# -*- coding: utf-8 -*-

from zope.interface import implements
from twisted.python import usage
from twisted.plugin import IPlugin
from twisted.application.service import IServiceMaker

from logtest.log import MyService

class Options(usage.Options):
    optParamaters = [[]]

class MyServiceMaker(object):
    implements(IServiceMaker, IPlugin)
    tapname = "logtest"
    description = "Twisted plugin log tester"
    options = Options

    def makeService(self, options):
        # set up servers and stuff here
        return MyService()

serviceMaker = MyServiceMaker()






More information about the Twisted-Python mailing list