Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

New-style formatting in the logging module (was Re: cpython (3.2): Issue #14123: Explicitly mention that old style % string formatting has caveats)

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


ncoghlan at gmail

Feb 26, 2012, 5:59 PM

Post #1 of 2 (146 views)
Permalink
New-style formatting in the logging module (was Re: cpython (3.2): Issue #14123: Explicitly mention that old style % string formatting has caveats)

On Mon, Feb 27, 2012 at 10:44 AM, Larry Hastings <larry [at] hastings> wrote:
> On 02/26/2012 03:33 PM, Guido van Rossum wrote:
>>
>> One thing I'd like to see happening regardless is support for
>> new-style formatting in the logging module. It's a little tricky to
>> think how that would work, alas -- should this be a property of the
>> logger or of the call?
>
>
> There already is some support.  logging.Formatter objects can be initialized
> with a "style" parameter, making this a property of the logger.  (New in
> 3.2.)
>
>   http://docs.python.org/py3k/library/logging.html#formatter-objects
>
> Is that what you had in mind?

It's half the puzzle (since composing the event fields into the actual
log output is a logger action, you know the style when you supply the
format string). The other half is that logging's lazy formatting
currently only supporting printf-style format strings - to use brace
formatting you currently have to preformat the messages, so you incur
the formatting cost even if the message gets filtered out by the
logging configuration. For that, a logger setting doesn't work, since
one logger may be shared amongst multiple modules, some of which may
use printf formatting, others brace formatting.

It could possibly be a flag to getLogger() though - provide a facade
on the existing logger type that sets an additional event property to
specify the lazy formatting style.

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan [at] gmail   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


vinay_sajip at yahoo

Feb 27, 2012, 1:24 AM

Post #2 of 2 (131 views)
Permalink
Re: New-style formatting in the logging module (was Re: cpython (3.2): Issue #14123: Explicitly mention that old style % string formatting has caveats) [In reply to]

Nick Coghlan <ncoghlan <at> gmail.com> writes:

> It's half the puzzle (since composing the event fields into the actual
> log output is a logger action, you know the style when you supply the
> format string). The other half is that logging's lazy formatting
> currently only supporting printf-style format strings - to use brace
> formatting you currently have to preformat the messages, so you incur
> the formatting cost even if the message gets filtered out by the
> logging configuration.

That isn't necessarily true. Lazy formatting can work for {} and $ formatting
types, not just %-formatting: see

http://plumberjack.blogspot.com/2010/10/supporting-alternative-formatting.html

Composing the event fields into the message is done by the LogRecord, which
calls str() on the object passed as the format string to get the actual format
string. This allows you to use any of the standard formatting schemes and still
take advantage of lazy formatting, as outlined in the above post.

Although style support for Formatters is new, that's really for merging the
logging event message into the overall log output (with time, severity etc.) -
the support for having your own way of formatting the event message has always
been there, even before str.format :-)

The Formatter style functionality is also available for 2.x through a separate
logutils project which I maintain and which contains features which were added
to logging in 3.2 such as QueueHandler/QueueListener:

http://pypi.python.org/pypi/logutils/

I will add a section in the logging cookbook about support for alternative
formatting styles. I thought I already had, but on inspection, it appears not to
be the case.

Regards,

Vinay Sajip

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.