[Python-Dev] Change to logging Formatters: support for alternative format styles

Vinay Sajip vinay_sajip at yahoo.co.uk
Sun Oct 31 15:35:56 CET 2010


Eric Smith <eric <at> trueblade.com> writes:

> I keep meaning to review this but haven't had time. One thing I want to 
> look at specifically is the ability to put the time formatting into the 
> str.format version of the format string. Now that the time format 
> specifier can be included in the format string, it's no longer necessary 
> to have the asctime inspection hack that's currently used in order to 
> avoid formatting the time. It would be good if we could remove the 
> formatTime logic for str.format, although I'm not sure how practical it 
> is. I suspect it's too baked-in to the design, but I'm hopeful.

Well, asctime checks would be in there for the other format styles anyway, and
you don't gain anything by making str.format a special case where the check is
avoided. The %-style will be around for a while, particularly as AFAICT
{}-formatting is still nominally slower than %-formatting [it *is* a lot more
flexible, so I can understand that]and people still cling to the "logging is
slow" meme, despite there being no hard numbers presented in evidence.

You don't have to specify asctime in the format string, but remember that since
Python logging predates datetime [both came in at Python 2.3 but the logging
package was independently available before, and compatible with Python 1.5.2 for
a while after introduction], times in logging are floats as per time.time().
IIUC you need a datetime.datetime object to do the formatting automatically
using str.format, so if one could e.g. use a Filter or LoggerAdapter to get a
datetime value into the LogRecord, then you could use {}-formatting without
{asctime} but with some other LogRecord attribute. I think the way it is now for
3.2 is the path of least resistance, though.

BTW as it is now, the asctime "hack" is less of a hack than it used to be; it
was an inline check before, but now can be easily reimplemented e.g. via
subclassing and overriding the usesTime() method.

Regards,

Vinay Sajip



More information about the Python-Dev mailing list