我的基于 Twisted 的客户端循环发送 UDP 数据包。因此,我使用的是 DatagramProtocol 类。这是来源:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from twisted.application.service import Service
from twisted.internet import reactor
from twisted.internet.task import LoopingCall
from twisted.internet.protocol import DatagramProtocol
from twisted.python import log
import logging
class HeartbeatClient(Service):
def __init__(self, host, port, data, beat_period):
self.ip = host
self.port = int(port)
self.data = data
self.beat = int(beat_period)
def startService(self):
self._call = LoopingCall(self._heartbeat)
self._call.start(self.beat)
def stopService(self):
self._call.stop()
def _heartbeat(self):
protocol = DatagramProtocol()
protocol.noisy = False
port = reactor.listenUDP(0, protocol)
port.write(self.data, (self.ip, self.port))
port.stopListening()
现在,当我使用 twistd 运行这个客户端时,我会从 Twisted 类(即 DatagramProtocol 类)中永久获取日志消息:
2011-09-11 18:39:25+0200 [-] (Port 55681 Closed)
2011-09-11 18:39:30+0200 [-] twisted.internet.protocol.DatagramProtocol starting on 44903
2011-09-11 18:39:30+0200 [-] (Port 44903 Closed)
2011-09-11 18:39:35+0200 [-] twisted.internet.protocol.DatagramProtocol starting on 50044
2011-09-11 18:39:35+0200 [-] (Port 50044 Closed)
2011-09-11 18:39:40+0200 [-] twisted.internet.protocol.DatagramProtocol starting on 37450
由于这些日志消息正在污染我的“自己的”日志,我想知道是否可以禁用这些日志消息。如您所见,我已经通过调用减少了日志数量protocol.noisy = False
,但我仍然收到其他日志消息。该命令g = protocol.ClientFactory().noisy = False
也无济于事。
是否可以以通用方式禁用所有 Twisted 内部类的日志记录 - 对于所有模块?也许通过使用一些 Twisted-logging 配置?