logging question

N

Neal D. Becker

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?
 
P

Peter Otten

Neal said:
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
 
V

Vinay Sajip

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top