<div dir="ltr"><div>> So, write:</div><div>></div><div>> LOG.debug(u'Could not do whatever you asked: %s', exc)</div><div>></div><div>> or just:</div><div>></div><div>> LOG.debug(exc)</div>
<div><br></div><div>Actually, that's a bad idea to pass an exception instance to</div><div>some log function: LOG.debug(exc). Let me show you why.</div><div><br></div><div>Here a snippet from logging.py:</div><div><br>
</div><div> def getMessage(self):</div><div> if not _unicode:</div><div> msg = str(self.msg)</div><div> else:</div><div> msg = self.msg</div><div> if not isinstance(msg, basestring):</div>
<div> try:</div><div> msg = str(self.msg)</div><div> except UnicodeError:</div><div> msg = self.msg # we keep exception object as it is</div><div> if self.args: # this condition is obviously False</div>
<div> msg = msg % self.args</div><div> return msg # returns an exception object, not a text</div><div><br></div><div>And here another snippet from the format() method:</div><div><br></div>
<div> record.message = record.getMessage()</div><div> # ... some time formatting ...</div><div> s = self._fmt % record.__dict__ # FAIL</div><div><br></div><div>the old string formatting will call str(), not unicode() and we will FAIL</div>
<div>with UnicodeEncodeError.</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, May 21, 2014 at 6:38 PM, Doug Hellmann <span dir="ltr"><<a href="mailto:doug.hellmann@dreamhost.com" target="_blank">doug.hellmann@dreamhost.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On Thu, May 15, 2014 at 11:29 AM, Victor Stinner<br>
<<a href="mailto:victor.stinner@enovance.com">victor.stinner@enovance.com</a>> wrote:<br>
> Hi,<br>
><br>
> I'm trying to define some rules to port OpenStack code to Python 3. I just<br>
> added a section in the "Port Python 2 code to Python 3" about formatting<br>
> exceptions and the logging module:<br>
> <a href="https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions" target="_blank">https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions</a><br>
><br>
> The problem is that I don't know what is the best syntax to log exceptions.<br>
> Some projects convert the exception to Unicode, others use str(). I also saw<br>
> six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message<br>
> contains a non-ASCII character).<br>
><br>
> IMO the safest option is to use str(exc). For example, use<br>
> LOG.debug(str(exc)).<br>
><br>
> Is there a reason to log the exception as Unicode on Python 2?<br>
<br>
</div>Exception classes that define translatable strings may end up with<br>
unicode characters that can't be converted to the default encoding<br>
when str() is called. It's better to let the logging code handle the<br>
conversion from an exception object to a string, since the logging<br>
code knows how to deal with unicode properly.<br>
<br>
So, write:<br>
<br>
LOG.debug(u'Could not do whatever you asked: %s', exc)<br>
<br>
or just:<br>
<br>
LOG.debug(exc)<br>
<br>
instead of converting explicitly.<br>
<span class="HOEnZb"><font color="#888888"><br>
Doug<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
><br>
> Victor<br>
><br>
> _______________________________________________<br>
> OpenStack-dev mailing list<br>
> <a href="mailto:OpenStack-dev@lists.openstack.org">OpenStack-dev@lists.openstack.org</a><br>
> <a href="http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev" target="_blank">http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev</a><br>
<br>
_______________________________________________<br>
OpenStack-dev mailing list<br>
<a href="mailto:OpenStack-dev@lists.openstack.org">OpenStack-dev@lists.openstack.org</a><br>
<a href="http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev" target="_blank">http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev</a><br>
</div></div></blockquote></div><br></div>