[openstack-dev] [All] Removing translations from debug logging where exception formatted into the message
doug.hellmann at dreamhost.com
Fri Jun 27 12:33:01 UTC 2014
On Thu, Jun 26, 2014 at 2:56 PM, Kuvaja, Erno <kuvaja at hp.com> wrote:
> We hit nasty situation where _() was removed from DEBUG level logging and
> there is exception included like following:
> msg = "Forbidden upload attempt: %s" % e
> This caused gettextutils raising UnicodeError:
> 2014-06-26 18:16:24.221 | File "glance/openstack/common/gettextutils.py",
> line 333, in __str__
> 2014-06-26 18:16:24.222 | raise UnicodeError(msg)
> 2014-06-26 18:16:24.222 | UnicodeError: Message objects do not support str()
> because they may contain non-ascii characters. Please use unicode() or
> translate() instead.
> (For Example
> As discussed with mriedm, jecarey and dhellmann on #openstack-oslo this can
> be avoided by specifying the message being unicode like:
> msg = u"Forbidden upload attempt: %s" % e
> For us in Glance it caused bunch of gating issues, so hopefully this helps
> the rest of the projects avoiding same, or at least tackling it bit faster.
> - Erno (jokke_) Kuvaja
Thanks for posting this.
We hope to have a release of oslo.i18n ready Real Soon Now, and as
part of that have started putting together more detailed instructions
for making the transition (see
The fix Erno describes above is a specific case of the new guideline
that we should treat all messages as unicode, not byte strings. This
isn't really a new thing, but it's going to become more important as
"lazy" translation is picked up by more projects. We were getting some
implicit type conversion behavior for free before because _() was
turning its str() input to a unicode() object by way of the lookup,
but that will not happen for log messages where _() isn't used (such
as the debug logging case).
The tl;dr: Use unicode everywhere, especially literal unicode strings
for log and error messages. Do not call str() on objects that might
include user-defined strings (like objects with names) or exceptions.
More information about the OpenStack-dev