Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2011 10:29
Show Gist options
  • Save binux/936418 to your computer and use it in GitHub Desktop.
Save binux/936418 to your computer and use it in GitHub Desktop.
logging receiver for python logging module
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cPickle
import logging
import logging.handlers
import SocketServer
import struct
class LogRecordStreamHandler(SocketServer.StreamRequestHandler):
"""Handler for a streaming logging request.
This basically logs the record using whatever logging policy is
configured locally.
def handle(self):
Handle multiple requests - each expected to be a 4-byte length,
followed by the LogRecord in pickle format. Logs the record
according to whatever policy is configured locally.
while 1:
chunk = self.connection.recv(4)
if len(chunk) < 4:
slen = struct.unpack(">L", chunk)[0]
chunk = self.connection.recv(slen)
while len(chunk) < slen:
chunk = chunk + self.connection.recv(slen - len(chunk))
obj = self.unPickle(chunk)
record = logging.makeLogRecord(obj)
def unPickle(self, data):
return cPickle.loads(data)
def handleLogRecord(self, record):
# if a name is specified, we use the named logger rather than the one
# implied by the record.
if self.server.logname is not None:
name = self.server.logname
name =
logger = logging.getLogger(name)
# N.B. EVERY record gets logged. This is because Logger.handle
# is normally called AFTER logger-level filtering. If you want
# to do filtering, do it at the client end to save wasting
# cycles and network bandwidth!
class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer):
"""simple TCP socket-based logging receiver suitable for testing.
allow_reuse_address = 1
def __init__(self, host='localhost',
SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler)
self.abort = 0
self.timeout = 1
self.logname = None
def serve_until_stopped(self):
import select
abort = 0
while not abort:
rd, wr, ex =[self.socket.fileno()],
[], [],
if rd:
abort = self.abort
def main():
format="%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s")
tcpserver = LogRecordSocketReceiver()
print "About to start TCP server..."
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment