[Twisted-Python] Modifying the logstring

Einar S. Idsø einar.twisted at norsk-esport.no
Mon Dec 6 04:21:57 EST 2010


Hi,

I have an application which logs activity on a number of servers by
two methods:
1. TCP-based queries (app is client)
2. UDP-based logging (app is server)

Each server that is being logged is represented by an instance of a
server class. TCP-queries are performed by each instance at set
intervals, while the UDP-logs are gathered by a global logging
instance which listens to a specific port. Events for a given server
can be triggered by both protocols, but are handled by the server
instance. In other words: An instance of a server class gathers events
by polling the server via TCP /and/ by receiving strings from a UDP
logging instance which passes received datagrams as strings to the
correct instance.

Whenever a loggable event results from the TCP queries, the name of
the protocol class, which is a subclass of
twisted.internet.protocol.Protocol), is used as the logstring. That is
fine, as it allows me to distinguish between different server classes,
although I cannot distinguish between individual servers.

However, when a loggable event occurs on the UDP logging instance, it
writes out a longish protocol name which is the same for all the
different server classes that are subscribing to events on that UDP
port. I have managed to modify the string by doing the following:

        self.udpLogger = reactor.listenUDP(myPort, UDPListener())
        self.udpLogger.logstr = "UDPLogger"

Yet when the logged string is passed, as a string, to a server
instance for processing, the logstring for any result remains
"UDPLogger" no matter how I pass it or what I do to it. I suppose it
is expected behaviour that the protocol should "follow" a result, no
matter how it is processed down the line, but is there any way in
which I can alter the logstring so that instead of "UDPLogger" or the
name of the protocol class, it instead uses the server id or any other
value that I specify?

What I am getting in my logs is:
2010-11-30 10:23:09+0100 [CSS,client] Something happened on TCP
2010-11-30 10:23:09+0100 [UDPLogger] Something happened on UDP

What I would like is:
2010-11-30 10:23:09+0100 [CSS (TCP),12345] Something happened on TCP
2010-11-30 10:23:09+0100 [CSS (UDP),12345] Something happened on UDP

where 12345 is a unique identifier of the specific server. Or at least:
2010-11-30 10:23:09+0100 [CSS,client] Something happened on TCP
2010-11-30 10:23:09+0100 [CSS] Something happened on UDP

so that I know the result from UDP concerns a CSS-server.

I hope this was possible to understand. Please let me know if anything
needs to be clarified. :)

Cheers,
Einar



More information about the Twisted-Python mailing list