Twisted:禁用 Twisted 框架类的日志记录

发布于 2024-12-04 01:47:25 字数 1739 浏览 1 评论 0原文

我的基于 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()

现在,当我使用 twind 运行此客户端时,我会永久从 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 配置?

My Twisted-based client sends UDP packets in a loop.
Therefore I'm using the class DatagramProtocol.
This is the source:

#!/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()

now when I run this client with twistd, I permanently get log messages from the Twisted classes, namely from the class 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

Since these log messages are polluting my "own" logs, I wonder if I can disable these log messages.
As you can see I already reduced the amount of logs by calling protocol.noisy = False, but I'm still getting other Log messages. Also the command g = protocol.ClientFactory().noisy = False does not help.

Is it possible to disable logging of all Twisted-internal classes, in a generic way - for ALL modules? Maybe by using some Twisted-logging configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2024-12-11 01:47:25

Twisted 的日志记录非常幼稚。但是,没有理由需要这样做,因为 twisted.python.log 功能非常丰富,并且能够进行您(和其他人)感兴趣的选择性报告

。事件只是任意键和值的字典。默认观察者知道带有 'message' 键的字典。也许正因为如此,Twisted 本身发出的大多数日志消息除了提供与此键关联的人类可读的字符串之外不会尝试做任何事情(而且,许多发出的消息是在当前 扭曲的日志系统,以及作为消费者的更旧、更原始的系统)。

不久前,这个问题困扰了某人,他被提示提交票证并开始处理解析为 问题的 UDP 部分尤其如此。该问题已基本解决,但仍有一些事情需要完成。

尝试的解决方案是记录一条结构化消息,该消息传达相同的信息,但没有消息,因此默认观察者不会记录。这可以避免消息默认出现在日志中,但允许对这些事件特别感兴趣的观察者观察它们并根据需要进行处理。

这张票已经有一段时间没有动过了。对于某人来说,拿起补丁并完成最后一步可能很容易。

Twisted's logging is all very naive. However, there is no reason this needs to be the case, since twisted.python.log is very featureful, and capable of the kind of selective reporting that you (and others) are interested in.

A log event is just a dictionary of arbitrary keys and values. The default observer knows about dictionaries with a 'message' key. Perhaps because of this, most log messages emitted by Twisted itself don't try to do anything except provide a human-readable string associated with this key (also, many of the emitted messages were added prior to the current Twisted logging system, and so had the older, more primitive system as a consumer).

Not too long ago, this problem bothered someone who was prompted to file a ticket and start working on a resolution to the UDP part of the problem in particular. The issue is mostly resolved, but a few things remain to be done.

The solution attempted is to log a structured message which conveys the same information, but has no message and so isn't recorded by the default observer. This avoids the messages appearing in the log by default, but allows an observer that's specifically interested in these events to observe them and handle them as desired.

The ticket has been sitting untouched for some time now. It would probably be easy for someone to pick up the patch and get it the last bit of the way to completion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文