Twisted:禁用Twisted-framework类的日志记录 [英] Twisted: disable logging of Twisted-framework classes

查看:94
本文介绍了Twisted:禁用Twisted-framework类的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基于Twisted的客户端循环发送UDP数据包. 因此,我正在使用DatagramProtocol类. 这是来源:

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()

现在,当我使用twisted运行此客户端时,我会从Twisted类(即从DatagramProtocol类)中永久获取日志消息:

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

由于这些日志消息正在污染我的自己的"日志,因此我想知道是否可以禁用这些日志消息. 如您所见,我已经通过调用protocol.noisy = False减少了日志数量,但是我仍然收到其他日志消息.同样,命令g = protocol.ClientFactory().noisy = False也无济于事.

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.

是否可以以通用方式对所有模块禁用所有Twisted内部类的日志记录?也许通过使用一些Twisted日志记录配置?

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?

推荐答案

Twisted的日志记录非常幼稚.但是,没有必要这么做,因为twisted.python.log具有很强的功能,并且能够进行您(和其他人)感兴趣的选择性报告.

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.

日志事件只是任意键和值的字典.默认观察者使用'message'键了解字典.也许正因为如此,Twisted本身发出的大多数日志消息除了提供与该键相关的人类可读字符串外都不做任何事情(此外,许多发出的消息是在 current 扭曲的日志记录系统,使用较旧,更原始的系统也是如此.

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).

不久前,这个问题困扰着有人,提示他提交一张票并开始正在努力解决问题的UDP部分.该问题已基本解决,但仍有一些事情要做.

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.

这篇关于Twisted:禁用Twisted-framework类的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆