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

Search Discussions

  • Vinay Sajip at Apr 21, 2010 at 8:34 am

    On Apr 19, 3:45?pm, J wrote:
    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
    {1:1,2:2}
    [1,2,3]
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 19, '10 at 2:45p
activeApr 21, '10 at 8:34a
posts2
users2
websitepython.org

2 users in discussion

J: 1 post Vinay Sajip: 1 post

People

Translate

site design / logo © 2022 Grokbase