我的基于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内部类的日志记录?也许可以使用一些扭曲日志记录配置?
发布于 2011-09-13 05:30:16
Twisted的日志记录非常幼稚。然而,没有理由需要这样做,因为twisted.python.log功能非常强大,并且能够提供您(和其他人)感兴趣的选择性报告。
日志事件只是一个包含任意键和值的字典。默认观察者知道带有'message'键的字典。也许正因为如此,Twisted本身发出的大多数日志消息除了提供与此键关联的人类可读字符串之外,不会尝试做任何事情(而且,许多发出的消息是在当前的Twisted日志记录系统之前添加的,因此旧的、更原始的系统作为使用者也是如此)。
不久前,这个问题困扰着一些人,他们被提示提交a ticket,并开始着手解决the UDP part of the problem的问题。这个问题基本上已经解决了,但还有一些事情要做。
尝试的解决方案是记录传达相同信息的结构化消息,但没有消息,因此不会被默认观察者记录。这避免了默认情况下出现在日志中的消息,但允许对这些事件特别感兴趣的观察者观察它们并根据需要处理它们。
这张票已经放了一段时间了。对于某个人来说,拿起补丁并完成最后一步可能很容易。
https://stackoverflow.com/questions/7379712
复制相似问题