logging.SocketHandler connections

oj ojeeves at gmail.com
Thu Nov 15 10:23:36 EST 2007


Hi folks,

I'm writing some fairly simple logging code that makes use of the
SocketHandler.

The example server code works fine, as expected. (http://
docs.python.org/lib/network-logging.html)

However, initially, I had tried it with a server that closed the
connection after receiving each record, and the SocketHandler doesn't
seem to behave as advertised.

My test script was simply this:

#!/usr/bin/python

import logging
import logging.handlers
import time
import sys

port = 12345

handler = logging.handlers.SocketHandler('localhost', port)
l = logging.getLogger("my-logger")
l.addHandler(handler)
l.addHandler(logging.StreamHandler(sys.stdout))
l.setLevel(logging.DEBUG)

for i in xrange(10):
	l.info("Log message %i", i)
	time.sleep(1)


My test server received messages 0, 3, 6 and 9.

Doing a packet capture with wireshark confirmed that it only made 4
connections.

The SocketHandler documentation says that it will re-establish the
connection if it has been closed. After a bit of digging, I found a
patch had been submitted and accepted that made it back off
exponentially. However, this should be time based. Even if I make my
sleep here 30 seconds, my server still only receives messages 0, 3, 6
and 9.

I'm concerned that if a connection is lost at some point, I will
always lose at least 2 log messages.

Is there some reason for this that I am not aware of? Have I
misunderstood something, or is this a bug?

-Oliver



More information about the Python-list mailing list