【发布时间】:2023-03-27 15:03:01
【问题描述】:
我的基于 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 配置?
【问题讨论】:
-
谢谢。不幸的是,答案不是这个问题的解决方案。即使我将协议上的嘈杂设置为 false(请参阅我对问题的补充),我仍然会收到日志消息。
-
日志消息来自
twisted.internet.udp.Port类,参见._bindSocket()和.connectionLost()方法。它没有提供禁用它的选项。 twistedmatrix.com/documents/current/core/howto/…
标签: python logging twisted twistd