<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>On Aug 5, 2010, at 12:44 AM, Valeriy Zamarayev wrote:</div><div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Hello, all.<br><br>There have been a few questions on this list about how to configure<br>Twisted logging via plugins, as opposed to .tac files. And we now know<br>about the #638 and #3534 tickets. Hopefully, they will be resolved<br>soon.<br><br>But before this happens, it seems to me there's no other way to handle<br>this than to use a "monkey patching" techinque on Twisted (excluding<br>patching the Twisted source code itself).<br><br>We need to access the 'application' object when the server starts. We<br>can do this when the 'twistd' code calls setServiceParent on the<br>service returned by the plugin. So if we wrap setServiceParent, we can<br>access the application object and do what we want.<br><br>See the code: &nbsp;<a href="http://gist.github.com/505926">http://gist.github.com/505926</a><br><br>This works for me. I don't know if a better and less dangerous way to<br>achieve the same exists. Maybe there is one?</div></blockquote></div><div><br></div><div>This actually looks pretty ingenious to me! &nbsp;It uses public APIs... mostly. &nbsp;I'd have to think pretty hard about whether this particular interface is actually <i>supported</i>, of course, since you're depending on extra stuff from your service parent which hypothetically may not actually be there.</div><div><br></div><div>If you're looking for aesthetic advice, it would be a bit nicer to use something like&nbsp;<a href="http://twistedmatrix.com/documents/10.1.0/api/twisted.python.components.html#proxyForInterface">proxyForInterface</a>&nbsp;and <i>wrap</i>&nbsp;the underlying service, rather than setting an attribute on it and clobbering its setServiceParent method.</div><div><br></div><div>Also... you could write a twistd plugin that could be used directly, instead of code that needs to be adapted / re-written for each different twistd plugin. &nbsp;Your code can scan the IServiceMaker plugin registry just as well as twistd itself can.</div><div><br></div><div>So you could have</div><div><br></div><div>&nbsp;&nbsp;twistd my-custom-logger --my-log-option-1 --my-log-option-2 web --path ~/public_html</div><div><br></div><div>and have the my-custom-logger plugin delegate to all twistd subcommands.</div><div><br></div><div>That's a pattern which should remain useful even after #638 is fixed.</div><div><br></div><div>Code to do this properly is left as an exercise for the reader, of course :).</div><div><br></div><div><br></div></body></html>