parameters to logging.debug()

W

Winston WOLFF

What do people think about changing the parameters to logging.debug()
to be more like the parameters to print? As you know, you can pass
multiple parameters to print, and they are all printed out with a space
between each. I find this more useful than the current method which
just takes the first parameter and submits it to the % operator with
the other parameters. From a coding point of view, I find it to better
to type this because all the strings are next to their labels:

log.debug("index=",i, "dict=", dict, "x=", x, "y=", y, "z=", z)

compared to this because the labels are all at the beginning, and you
have to spend some time keeping things in the right order:

log.debug( "index=%i dict=%s x=%d y=%d z=%d", i, dict, x, y, z)

Also, for those who like the second format better, you can always type:
log.debug( "index=%i dict=%s x=%d y=%d z=%d" % ( i, dict, x, y, z) )

I know it might be a little late to make such a drastic change to the
logging API, but I really think it would be worthwhile. Here are my
reasons summarized again:

Why should we change the log.debug/info/warn/error() API:
- It will match the print statement, which is what many people use already
- It keeps the labels next to the variables you are printing. This
makes it easier to change which variables you are printing, as one does
when debugging.
- If you like the current format, it is very easily done using the %
operator with no loss of clarity.
- A list of parmeters that are just printed out after converting with
repr() is less error prone than what we have now. If you mess up the
sequence of %i, %s, etc, then you might pass a string to your %i
parameter but not notice until your debug message is actually called.

-Winston
 
B

Brian

Winston said:
- If you like the current format, it is very easily done using the %
operator with no loss of clarity.

But what about loss of performance? I don't want string formatting done
if the debugging output isn't even going to make it to the handler.
-Brian
 
M

Michael Hoffman

Winston said:
What do people think about changing the parameters to logging.debug() to
be more like the parameters to print?

I think it will break a lot of software that uses the current API.
Also, for those who like the second format better, you can always type:
log.debug( "index=%i dict=%s x=%d y=%d z=%d" % ( i, dict, x, y, z) )

That argument could also be made about your proposal. You can always type:

log.debug(" ".join(map(str, ("index=",i, "dict=", dict, "x=", x, "y=",
y, "z=", z))))

(Isn't Python going to start auto-stringizing the items in a join
sometime soon?)

Or better yet, you can subclass your own logger that does this
automatically. That's one of the reasons the logging stuff is so
extensible ;-)
 

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
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top