[openstack-dev] Use of local()

Jason Kölker jason at koelker.net
Thu Feb 7 22:26:08 UTC 2013


On Thu, Feb 7, 2013 at 3:05 PM, Sean Dague <sdague at linux.vnet.ibm.com> wrote:
> On 02/07/2013 03:32 PM, Mark McLoughlin wrote:
>>
>>    _("The server with id %(s_id)s has no key %(m_key)s") %
>> dict(s_id="1234", m_key="imageId")
>
>
> Well, it's actually
>
> (_("The server with id %(s_id)s has no key %(m_key)s") %
>
>  dict(s_id="1234", m_key="imageId")
>
> because of line length.
>
> I'm not a huge fan of locals(), and would love to get rid of it largely to
> stop making pyflakes light up my emacs buffers errantly, but realize we're
> going to have a lot more line wrapping in the process.

+1 on getting rid of the locals() magic. We could easily add in an
adapter to the oslo log that would let us just pass in arbitrary
kwargs. Something like:

import logging
import sys


_ = lambda x: ' '.join(reversed(x.split()))


class StringFormattingAdapter(logging.LoggerAdapter):
    def process(self, msg, kwargs):
        msg = msg % kwargs
        new_kwargs = dict((k, v) for k, v in kwargs.iteritems()
                          if k in ('exc_info', 'extra'))
        return msg, new_kwargs


logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
LOG = StringFormattingAdapter(logging.getLogger("test"), {})
LOG.debug(_("Hi %(name)s"), name="everybody")

Obviously would be better to autodiscover the kwargs allowed into
_log(), but you get the gist.  If peeps like it I can work on a patch,
the question being should this be the default behavior of the
getLogger and should it be incorporated into the ContextAdapter or
wrap that adapter. Thoughts?

Happy Hacking!

7-11



More information about the OpenStack-dev mailing list