logging question

Peter Otten __peter__ at web.de
Thu Oct 28 15:43:14 CEST 2004


Neal D. Becker wrote:

> Why isn't my formatter used when I use DatagramHandler?

I'd say your formatting efforts happen on the wrong end of the connection.
What is actually sent are LogRecord objects, not formatted messages. This
gives you more flexibility, as you can add as many handlers (possibly with
different formatters) as you may wish. Below is a modification of your
"server" along these lines:

#!/usr/bin/env python

import socket
import logging
import pickle

sock = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
sock.bind (("", 8881))

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s
%(message)s'))
logger = logging.getLogger("fromFarAway")
logger.addHandler(handler)
    

try:
    while True:
        data, address = sock.recvfrom(8192)
        rec = logging.makeLogRecord(pickle.loads(data[4:]))
        logger.handle(rec)

finally:
    sock.close()

Whether there is a way to automatically replicate a logger hierarchy I don't
know.

Peter




More information about the Python-list mailing list