Search code examples
pythonloggingtwistedpython-loggingtwistd

Twisted: disable logging of Twisted-framework classes


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?


Solution

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