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

J dreadpiratejeff at gmail.com
Mon Apr 19 10:45:53 EDT 2010


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

>>> {1:1,2:2}
>>> [1,2,3]

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?



More information about the Python-list mailing list