<div dir="ltr"><div>Hi,</div><div><br></div><div>> Is there a reason to log the exception as Unicode on Python 2?</div><div><br></div><div>Sure, why not? Some exceptions may have unicode message with non-ASCII</div><div>
characters. and we obviously can't cast such exceptions with str() in</div><div>Python 2.x.</div><div><br></div><div><br></div><div>> The problem is that I don't know what is the best syntax to log exceptions.</div>
<div><br></div><div>I hate this unicode dances too, since I don't understand all nuances and</div><div>potential pitfalls. But I belive the better approach is to use unicode()</div><div>for Python 2.x and str() for Python 3.x.</div>
<div><br></div><div>Example:</div><div><br></div><div>    LOG.error(six.text_type(e))</div><div><br></div><div><br></div><div>P.S: I've a quick look over logging implementaion, and figured out</div><div>that it has some code to dial with unicode. In few words, if we know</div>
<div>about exception's attributes, it's better to use it directly to log:</div><div><br></div><div>Example:</div><div><br></div><div>    LOG.error(e.message)</div><div><br></div><div><br></div><div>- Igor</div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Thu, May 15, 2014 at 6:29 PM, Victor Stinner <span dir="ltr"><<a href="mailto:victor.stinner@enovance.com" target="_blank">victor.stinner@enovance.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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>
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>
</blockquote></div><br></div></div>