Anuvrat Parashar Anuvrat Parashar - 3 months ago 14
Python Question

How to suppress noisy factory started/stopped log messages from Twisted?

I am using twisted.web.client.Agent which in turn is using HTTP11ClientProtocol. The innumerable factory started / stopped messages are obscuring the messages that I am actually interested in. So, I am looking for a way to suppress them. Is there any?

2013-09-07 11:03:15+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x3183638>
2013-09-07 11:03:15+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x316d050>
2013-09-07 11:03:15+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x3183e18>
2013-09-07 11:03:16+0530 [HTTP11ClientProtocol,client] <twisted.web._newclient.Response object at 0x3185150>
2013-09-07 11:03:16+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x3183638>
2013-09-07 11:03:16+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x2dafa70>
2013-09-07 11:03:16+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x3184950>
2013-09-07 11:03:16+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x317c170>
2013-09-07 11:03:16+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x3186098>
2013-09-07 11:03:17+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x3171248>
2013-09-07 11:03:17+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x313ef80>
2013-09-07 11:03:17+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x311dbd8>
2013-09-07 11:03:17+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x31867a0>
2013-09-07 11:03:18+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x3171f80>
2013-09-07 11:03:18+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x3171ea8>
2013-09-07 11:03:18+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x311de60>
2013-09-07 11:03:18+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x316d4d0>
2013-09-07 11:03:19+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x325a830>
2013-09-07 11:03:19+0530 [-] Starting factory <twisted.web.client._HTTP11ClientFactory instance at 0x325eb90>
2013-09-07 11:03:20+0530 [HTTP11ClientProtocol,client] Stopping factory <twisted.web.client._HTTP11ClientFactory instance at 0x3183e18>


This (Twisted starting/stopping factory/protocol less noisy log messages) is a similar question, but since HTTP11ClientProtocol is not something that I am instantiating, I can't figure out where I would set the property noisy to false.

Answer

If you want every _HTTP11ClientFactory in the application to hush up, you could just set the noisy property at the class level:

from twisted.web import client
client._HTTP11ClientFactory.noisy = False

A more flexible approach would be to create a custom HTTPConnectionPool that uses quiet factories and pass that to your Agent when you want a quieter log:

from twisted.web import client

class QuietHTTP11ClientFactory(client._HTTP11ClientFactory):
    noisy = False

myQuietPool = client.HTTPConnectionPool(reactor)
myQuietPool._factory = QuietHTTP11ClientFactory

agent = client.Agent(reactor, pool=myQuietPool)