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

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()

On 21 Oct, 05:59 am, general@vultaire.net wrote:
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?
This is a limitation of the implementation of twistd's plugin support. tac files can define custom log observers. Plugins for twistd cannot. Ticket #638 was originally for all of the custom logging feature, but it was eventually split up and the tac parts moved to a separate ticket, #3534, which is now resolved. #638 is still open, however. #3538 is another related ticket which proposes an alternate solution to the problem, allowing tacs and plugins to be used in combination. Jean-Paul
participants (2)
-
exarkun@twistedmatrix.com
-
general@vultaire.net