如何抑制嘈杂的工厂启动/停止来自Twisted的日志消息?

时间:2013-09-07 05:56:09

标签: python logging twisted

我正在使用twisted.web.client.Agent,而后者又使用HTTP11ClientProtocol。无数工厂启动/停止的消息掩盖了我实际感兴趣的消息。所以,我正在寻找一种方法来抑制它们。有没有?

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>

这个(Twisted starting/stopping factory/protocol less noisy log messages)是一个类似的问题,但由于HTTP11ClientProtocol不是我实例化的东西,我无法弄清楚我将把属性设置为false的位置。

2 个答案:

答案 0 :(得分:12)

如果您希望应用程序中的每个_HTTP11ClientFactory都能够提升,那么您可以在类级别设置noisy属性:

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

更灵活的方法是创建一个使用安静工厂的自定义HTTPConnectionPool,并在需要更安静的日志时将其传递给Agent

from twisted.web import client

class QuietHTTP11ClientFactory(client._HTTP11ClientFactory):
    noisy = False

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

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

答案 1 :(得分:0)

我只能在日志记录级别执行此操作:

import logging
logging.getLogger('twisted').setLevel(logging.CRITICAL)