Is it better to extend a class, or do something repetitious in themain part of a program?

J

J

First, before I get farther,

Is there a way for the logging module to natively handle lists and
dict objects when logging?

e.g. take this {'key1':'val1','key2':'val2'} and have it logged like this:

INFO: key1: val1
INFO: key2: val2

If I pass the dict or list directly to the logger, it is logged the
same as if you simply did this:

mydict={1:1, 2:2}
mylist=[1,2,3]

print mydict
print mylist

It came up that I wanted to have logging present command line options
from optparse if the log level was set to debug... so they'd look
something like this in the output:

debug: True
sleep_time: 30
log_file: /tmp/testlog

So the options I've managed to work out are to either parse the list
or dict object item by item and feed those items one at a time into
the logger:

for i in mylist:
logging.info(i)

Or to extend the StreamHandler class to handle this by creating a new
report.msg...

Then the discussion came up: which is better? To parse a dictionary
or list in the main code and pass each item to the logger one at a
time, or to extend the logger to handle it natively?

Any thoughts on which is the more proper way to handle cases like this?
 
V

Vinay Sajip

First, before I get farther,

Is there a way for theloggingmodule to natively handle lists and
dict objects whenlogging?

e.g. take this {'key1':'val1','key2':'val2'} and have it logged like this:

INFO: key1: val1
INFO: key2: val2

If I pass the dict or list directly to the logger, it is logged the
same as if you simply did this:

mydict={1:1, 2:2}
mylist=[1,2,3]

print mydict
print mylist

It came up that I wanted to haveloggingpresent command line options
from optparse if the log level was set to debug...  so they'd look
something like this in the output:

debug: True
sleep_time: 30
log_file: /tmp/testlog

So the options I've managed to work out are to either parse the list
or dict object item by item and feed those items one at a time into
the logger:

for i in mylist:
   logging.info(i)

Or to extend the StreamHandler class to handle this by creating a new
report.msg...

Then the discussion came up: which is better?  To parse a dictionary
or list in the main code and pass each item to the logger one at a
time, or to extend the logger to handle it natively?

Any thoughts on which is the more proper way to handle cases like this?

Since you want to have a specific output format for the logged
information, the best way to go would be to write your own Formatter
subclass, and check in its format() method whether you have lists/
dicts and then format them however you want into a string, and return
the appropriately formatted string from that method.

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top