Created
August 3, 2010 06:08
-
-
Save valeryz/505926 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ==== twisted/plugins/finger_plugin.py ==== | |
# This snippet shows how to configure Twisted plugins to use custom logging. | |
# This is a modification of a 'finger' example by Chris Miles at | |
# http://chrismiles.livejournal.com/23399.html | |
# | |
# It's kind of a dirty monkey patching hack, but I don't know how to | |
# configure logging in plugins until this ticket is resolved: | |
# http://twistedmatrix.com/trac/ticket/638 | |
# | |
# I change logging from the default LogFile to DailyLogFile, and also | |
# install a custom log observer. | |
# - Zope modules - | |
from zope.interface import implements | |
# - Twisted modules - | |
from twisted.python import usage | |
from twisted.application.service import IServiceMaker | |
from twisted.plugin import IPlugin | |
from twisted.python.log import ILogObserver, FileLogObserver, textFromEventDict | |
from twisted.python.logfile import DailyLogFile | |
from twisted.python import util | |
# - Finger modules - | |
from finger import finger | |
class Options(usage.Options): | |
synopsis = "[options]" | |
longdesc = "Make a finger server." | |
optParameters = [ | |
['file', 'f', '/etc/users'], | |
['templates', 't', '/usr/share/finger/templates'], | |
['ircnick', 'n', 'fingerbot'], | |
['ircserver', None, 'irc.freenode.net'], | |
['pbport', 'p', 8889], | |
# adding new 'logfile' option. The original -l options of twistd | |
# will not be used | |
['logfile', 'l', 'finger.log'], | |
] | |
optFlags = [['ssl', 's']] | |
class MyLogObserver(FileLogObserver): | |
# This is my custom log observer. What it does is it avoids printing | |
# the 'system' component of the log | |
# It can basically do whatever you want with your logging | |
def emit(self, eventDict): | |
text = textFromEventDict(eventDict) | |
if text is None: | |
return | |
timeStr = self.formatTime(eventDict['time']) | |
util.untilConcludes(self.write, timeStr + " " + text.replace("\n", "\n\t") + "\n") | |
util.untilConcludes(self.flush) | |
# wrapper for the setServiceParent method of t.a.s.Service | |
def wrap_setServiceParent(func, config): | |
def decorator(app): | |
logfile = DailyLogFile(config["logfile"], "/tmp") | |
app.setComponent(ILogObserver, MyLogObserver(logfile).emit) | |
return func(app) | |
return decorator | |
class MyServiceMaker(object): | |
implements(IServiceMaker, IPlugin) | |
tapname = "finger" | |
description = "Finger server." | |
options = Options | |
def makeService(self, config): | |
s = finger.makeService(config) | |
# Here's the monkey patching trick: | |
# we hook into setServiceParent of the service to execute some code | |
# in the context where twisted application object is accessible | |
s.setServiceParent = wrap_setServiceParent(s.setServiceParent, config) | |
return s | |
serviceMaker = MyServiceMaker() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment