[Twisted-Python] UDP Logging Server

SIC FS LIST sicfslist at gmail.com
Fri Mar 11 14:15:47 EST 2011


I am trying to write a UDP based logging server.

Generically speaking it looks somewhat like syslog except I needed a bit
more flexibility that syslog can provide (or at least that I think it can

What I'm trying to accomplish is:
-- receive UDP packet
-- parse UDP packet
-- write output to a log file
-- have the log files rotated on a periodic basis

So far I have a "working" implementation ... but I'm noticing that if I do
the following:
-- log when a message is received
-- that for that message it "might" show up in the file a pretty lengthy
period of time later

The actual UDP protocol:

class VocsLogger(DatagramProtocol):
    def datagramReceived(self, data, (host, port)):
        _proc_msg(self.transport, data, (host,

The _proc_msg class:

class _proc_msg:
    def __init__(self, sck, data, (host, port)):
        self._sck = sck
        self._data = data
        self._host = host
        self._port = port

    def _new_msg(self):
        d, _ = LogMsg().ParseSocketMsg(self._data)
        if d.type.upper() == DISKINFO[0]:
            DISKINFO[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))
        elif d.type.upper() == LOADAVG[0]:
            LOADAVG[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))
        elif d.type.upper() == MEMINFO[0]:
            MEMINFO[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))
        elif d.type.upper() == NETDEV[0]:
            NETDEV[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))
        elif d.type.upper() == PSAUX[0]:
            PSAUX[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))
        elif d.type.upper() == WHOINFO[0]:
            WHOINFO[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))
            DEFAULT[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))

And I have a log rotate service that looks like this:

class LogRotateService(TimerService):
    def __init__(self):
        TimerService.__init__(self, 60 * 5, LogRotate)

And then I'm using twistd to actually make it work:

application = service.Application("vocs-logger")
rotateLogService = LogRotateService()
loggerService = internet.UDPServer(int(config['port']), VocsLogger(),

I'm probably approaching this the wrong way and not sure if I sure really
worry about deferring the actual process of writing to the log file or if
there was a better way altogether.

Thanks for any guidance.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20110311/c8807889/attachment.htm 

More information about the Twisted-Python mailing list