pythonloggingtwistedtwistd

How to set twistd.py ILogObserver when using IPlugin?


I want to redirect twistd.py's logging to python's logging. When starting a .tac file normally I can easily do this:

from twisted.python.log import PythonLoggingObserver, ILogObserver
from twisted.application import service

application = service.Application("FooApp")
application.setComponent(ILogObserver, PythonLoggingObserver().emit)

However, I don't seem to have an Application when writing an IPlugin. Instead I just have a class that implements IServiceMaker and IPlugin, where makeService returns a service.Service. How can I set this log observer?

Note that I don't just want to add a python logging observer, I want to redirect twistd's logging so that it only goes through python's built-in logging system.


Solution

  • Look at twistd's --logger parameter:

    # mylogger.py
    from twisted.python import log
    
    def logger():
        return log.PythonLoggingObserver().emit
    

    and then invoke twistd: twistd --logger=mylogger.logger.