By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

logging question

P: n/a
Why isn't my formatter used when I use DatagramHandler?

Here's the server:
#!/usr/bin/env python

import socket
import logging
import pickle

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

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

finally:
sock.close()

Here's the client:

import logging
import logging.handlers
import sys
import os

logger = logging.getLogger()
hdlr = logging.handlers.DatagramHandler ('rpppc1.md.hns.com', 8881)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

import time

while (True):
logger.info (str (sys.argv)+":"+str(os.getpid())+":"+"hi")
time.sleep (30)

Here's what gets printed:
['./logclient.py']:23913:hi ()

What happened to the formatter?

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
> Whether there is a way to automatically replicate a logger hierarchy I don't
know.


Peter is right about the formatting. I'm not sure what's meant by
'replicate a logger hierarchy' - if you mean complete with handlers,
formatters, etc. then the only ways of doing this are by using the
same API calls on both sides, or loading the configuration of both
sides from a shared configuration file. If you are sending logging
events across a network, then the normal configuration is to just have
a socket or datagram handler on the source side of the connection, and
on the sink side the messages are handled as per Peter's code -
formatted, written to file, emailed or whatever. However, you can use
any logger name you like on the receiving side - the one that came in
on the LogRecord, or something prepended/appended to the logger name
in the LogRecord.

Regards,
Vinay Sajip
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.