[openstack-dev] [OpenStack-Dev] Logging formats and i18n
Doug Hellmann
doug at doughellmann.com
Mon Dec 22 18:13:27 UTC 2014
On Dec 22, 2014, at 1:05 PM, John Griffith <john.griffith8 at gmail.com> wrote:
> On Mon, Dec 22, 2014 at 10:03 AM, Ben Nemec <openstack at nemebean.com> wrote:
>> On 12/22/2014 09:42 AM, John Griffith wrote:
>>> Lately (on the Cinder team at least) there's been a lot of
>>> disagreement in reviews regarding the proper way to do LOG messages
>>> correctly. Use of '%' vs ',' in the formatting of variables etc.
>>>
>>> We do have the oslo i18n guidelines page here [1], which helps a lot
>>> but there's some disagreement on a specific case here. Do we have a
>>> set answer on:
>>>
>>> LOG.info(_LI('some message: v1=%(v1)s v2=%(v2)s') % {'v1': v1, 'v2': v2})
>>>
>>> vs
>>>
>>> LOG.info(_LI('some message: v1=%(v1)s v2=%(v2)s'), {'v1': v1, 'v2': v2})
>>
>> This is the preferred way.
>>
>> Note that this is just a multi-variable variation on
>> http://docs.openstack.org/developer/oslo.i18n/guidelines.html#adding-variables-to-log-messages
>> and the reasoning discussed there applies.
>>
>> I'd be curious why some people prefer the % version because to my
>> knowledge that's not recommended even for untranslated log messages.
>
> Not sure if it's that anybody has a preference as opposed to an
> interpretation, notice the recommendation for multi-vars in raise:
>
> # RIGHT
> raise ValueError(_('some message: v1=%(v1)s v2=%(v2)s') % {'v1': v1, 'v2': v2})
It’s really not related to translation as much as the logging API itself.
With the exception, you want to initialize the ValueError instance with a proper message as soon as you throw it because you don’t know what the calling code might do with it. Therefore you use string interpolation inline.
When you call into the logging subsystem, your call might be ignored based on the level of the message and the logging configuration. By letting the logging code do the string interpolation, you potentially skip the work of serializing variables to strings for messages that will be discarded, saving time and memory.
These “rules” apply whether your messages are being translated or not, so even for debug log messages you should write:
LOG.debug(‘some message: v1=%(v1)s v2=%(v2)s’, {‘v1’: v1, ‘v2’: v2})
>
>>
>>>
>>>
>>> It's always fun when one person provides a -1 for the first usage; the
>>> submitter changes it and another reviewer gives a -1 and says, no it
>>> should be the other way.
>>>
>>> I'm hoping maybe somebody on the olso team can provide an
>>> authoritative answer and we can then update the example page
>>> referenced in [1] to clarify this particular case.
>>>
>>> Thanks,
>>> John
>>>
>>> [1]: http://docs.openstack.org/developer/oslo.i18n/guidelines.html
>>>
>>> _______________________________________________
>>> OpenStack-dev mailing list
>>> OpenStack-dev at lists.openstack.org
>>> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>>>
>>
>>
>> _______________________________________________
>> OpenStack-dev mailing list
>> OpenStack-dev at lists.openstack.org
>> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
>
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
More information about the OpenStack-dev
mailing list