String concatenation vs. string formatting

Andrew Berg bahamutzero8825 at gmail.com
Sat Jul 9 18:02:32 EDT 2011


On 2011.07.09 06:06 AM, Vinay Sajip wrote:
> In a logging context at least, using the form like
>
> logger.debug("formatting message with %s", "arguments")
>
> rather than
>
> logger.debug("formatting message with %s" % "arguments")
How would I do that with the newer formatting? I've tried:
> logger.info('Binary preset file {file} successfully stored.', {file :
> queue[0].preset_file})
(global name 'file' not defined)
and
> logger.info('Binary preset file {file} successfully stored.',
> file=queue[0].preset_file)
(unexpected keyword 'file')

> By the way, logging primarily uses %-formatting instead of the newer
> {}-formatting, because it pre-dates {}-formatting. In more recent versions of
> Python, all of Python's three formatting styles are supported - see
I've noticed. :-)
> log_formatter = logging.Formatter('{asctime} - __main__ - {funcName} -
> line {lineno} - {levelname} - {message}', style='{')




More information about the Python-list mailing list