473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

logging question

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
2 2437
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: chuck | last post by:
I want to create two different loggers so that users see one output (e.g. no exception/stack traces) and others (e.g support staff) that does see stack traces via email. The code below is close...
1
by: s99999999s2003 | last post by:
hi i have defined a function def logger(logfile,msg): import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y...
3
by: guybenron | last post by:
Hey, I have a sort of petty-neurotic question, I'm kinda pedantic and like to keep my log directories clean, and this thing is bothering me to the point of actually posting a question (couldn't...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
4
by: Alexandru Mosoi | last post by:
why doesn't logging throw any exception when it should? how do I configure logging to throw exceptions? .... logging.fatal('asdf %d', '123') .... except: .... print 'this line is never...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.