Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On Thu, May 15, 2014 at 11:29 AM, Victor Stinner victor.stin...@enovance.com wrote: Hi, I'm trying to define some rules to port OpenStack code to Python 3. I just added a section in the Port Python 2 code to Python 3 about formatting exceptions and the logging module: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions The problem is that I don't know what is the best syntax to log exceptions. Some projects convert the exception to Unicode, others use str(). I also saw six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message contains a non-ASCII character). IMO the safest option is to use str(exc). For example, use LOG.debug(str(exc)). Is there a reason to log the exception as Unicode on Python 2? Exception classes that define translatable strings may end up with unicode characters that can't be converted to the default encoding when str() is called. It's better to let the logging code handle the conversion from an exception object to a string, since the logging code knows how to deal with unicode properly. So, write: LOG.debug(u'Could not do whatever you asked: %s', exc) or just: LOG.debug(exc) instead of converting explicitly. Doug Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
So, write: LOG.debug(u'Could not do whatever you asked: %s', exc) or just: LOG.debug(exc) Actually, that's a bad idea to pass an exception instance to some log function: LOG.debug(exc). Let me show you why. Here a snippet from logging.py: def getMessage(self): if not _unicode: msg = str(self.msg) else: msg = self.msg if not isinstance(msg, basestring): try: msg = str(self.msg) except UnicodeError: msg = self.msg # we keep exception object as it is if self.args: # this condition is obviously False msg = msg % self.args return msg # returns an exception object, not a text And here another snippet from the format() method: record.message = record.getMessage() # ... some time formatting ... s = self._fmt % record.__dict__ # FAIL the old string formatting will call str(), not unicode() and we will FAIL with UnicodeEncodeError. On Wed, May 21, 2014 at 6:38 PM, Doug Hellmann doug.hellm...@dreamhost.comwrote: On Thu, May 15, 2014 at 11:29 AM, Victor Stinner victor.stin...@enovance.com wrote: Hi, I'm trying to define some rules to port OpenStack code to Python 3. I just added a section in the Port Python 2 code to Python 3 about formatting exceptions and the logging module: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions The problem is that I don't know what is the best syntax to log exceptions. Some projects convert the exception to Unicode, others use str(). I also saw six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message contains a non-ASCII character). IMO the safest option is to use str(exc). For example, use LOG.debug(str(exc)). Is there a reason to log the exception as Unicode on Python 2? Exception classes that define translatable strings may end up with unicode characters that can't be converted to the default encoding when str() is called. It's better to let the logging code handle the conversion from an exception object to a string, since the logging code knows how to deal with unicode properly. So, write: LOG.debug(u'Could not do whatever you asked: %s', exc) or just: LOG.debug(exc) instead of converting explicitly. Doug Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On Wed, May 21, 2014, John Dennis jden...@redhat.com wrote: But that's a bug in the logging implementation. Are we supposed to write perverse code just to avoid coding mistakes in other modules? Why not get the fundamental problem fixed? It has been fixed, by making Python 3 :) This is a problem in the Python 2 standard library. I agree it kind of sucks. We've traditionally just worked around it, but monkey patching might be a solution if the work arounds are onerous. JE ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On 05/21/2014 12:11 PM, Igor Kalnitsky wrote: So, write: LOG.debug(u'Could not do whatever you asked: %s', exc) or just: LOG.debug(exc) Actually, that's a bad idea to pass an exception instance to some log function: LOG.debug(exc). Let me show you why. Here a snippet from logging.py: def getMessage(self): if not _unicode: msg = str(self.msg) else: msg = self.msg if not isinstance(msg, basestring): try: msg = str(self.msg) except UnicodeError: msg = self.msg # we keep exception object as it is if self.args: # this condition is obviously False msg = msg % self.args return msg # returns an exception object, not a text And here another snippet from the format() method: record.message = record.getMessage() # ... some time formatting ... s = self._fmt % record.__dict__ # FAIL the old string formatting will call str(), not unicode() and we will FAIL with UnicodeEncodeError. This is exactly why we had to do https://github.com/openstack/oslo-incubator/blob/master/openstack/common/log.py#L344 As long as it gets passed in as unicode in the first place the logging code handles it fine, but if it gets passed in as a generic object it will blow up. @Doug: It occurs to me that this might be a problem with our plan to stop using the ContextAdapter. :-/ On Wed, May 21, 2014 at 6:38 PM, Doug Hellmann doug.hellm...@dreamhost.comwrote: On Thu, May 15, 2014 at 11:29 AM, Victor Stinner victor.stin...@enovance.com wrote: Hi, I'm trying to define some rules to port OpenStack code to Python 3. I just added a section in the Port Python 2 code to Python 3 about formatting exceptions and the logging module: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions The problem is that I don't know what is the best syntax to log exceptions. Some projects convert the exception to Unicode, others use str(). I also saw six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message contains a non-ASCII character). IMO the safest option is to use str(exc). For example, use LOG.debug(str(exc)). Is there a reason to log the exception as Unicode on Python 2? Exception classes that define translatable strings may end up with unicode characters that can't be converted to the default encoding when str() is called. It's better to let the logging code handle the conversion from an exception object to a string, since the logging code knows how to deal with unicode properly. So, write: LOG.debug(u'Could not do whatever you asked: %s', exc) or just: LOG.debug(exc) instead of converting explicitly. Doug Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
*@Victor:* six.text_type(exc): always use Unicode. It may raise unicode error depending on the exception, be careful. Example of such error in python 2: unicode(Exception(nonascii:\xe9)). It's very rare situation. Actually, it's no sense since we know nothing about exception's encoding. Without encoding it's nothing more, but bytes, while logging is a text stream first of all. So I agree with @Johannes here. We need to use unicode for text streams, or at least ASCII-compatible str objects. *@Johannes:* If a third party library raises exceptions with strings in an encoding other than ASCII, they should be shot :) +1 I don't understand the problem. What are you expecting from unicode(exc)? What exceptions don't meet that expectation? Sorry if you misunderstood me. My point is sometimes str() or unicode() don't give us the result we expecting for because of user's exception hierarchy. For instance, web.py's exception just shows you a status code, and doesn't print the exception's message. The problem is in library, not in approach, but shit happens and I don't see problems with using direct access to attribute, if library's documentation gives a grantee that we can use attributes for getting exception message. In that case, I propose not to do an explicit conversion, and leave this task for logging module. - Igor On Sun, May 18, 2014 at 2:03 AM, Johannes Erdfelt johan...@erdfelt.comwrote: On Fri, May 16, 2014, Igor Kalnitsky ikalnit...@mirantis.com wrote: unicode(exc) (or six.text_type(exc)) works for all exceptions, built-in or custom. That's too much of a statement. Sometimes exceptions implement their own __str__ / __unitcode__ methods, that return too many rubbish information or not enough. What do you do in that case? I don't understand the problem. What are you expecting from unicode(exc)? What exceptions don't meet that expectation? Using str(exc) or unicode(exc) is the standard Python convention to get a useful string out of an exception. This is what the traceback module does (at least in Python 2.7) for the last line in the traceback output. It tries str first, then unicode if str fails. If it uses unicode, then it backslash escapes it back to ASCII. The behavior to try str first and then unicode second appears to be because of legacy reasons. I'm not entirely sure why it backslash escapes unicode back to ASCII. Maybe to avoid a possible second exception when printing it? JE ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On 05/16/2014 09:11 AM, Johannes Erdfelt wrote: six.text_type(exc) is the recommended solution. +1 -- John ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On Fri, May 16, 2014, Victor Stinner victor.stin...@enovance.com wrote: See my documentation: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions six.text_type(exc): always use Unicode. It may raise unicode error depending on the exception, be careful. Example of such error in python 2: unicode(Exception(nonascii:\xe9)). unicode(exc) works with such exception classes: --- class MyException1(Exception): pass exc = MyException1() exc.message = u\u20ac unicode(exc) #ok class MyException2(Exception): def __unicode__(self): return u\20ac exc = MyException2() unicode(exc) #ok --- If we want to format an exception as Unicode, we need a function trying unicode(), or use str() and then guess the encoding. It means adding a new safe function to Olso to format an exception. This is unnecessarily complicated. Strings should be decoded to unicode as soon as possible. When a request is read from a user, it should be decoded to a unicode type. When it's read from a file, it should be decoded to a unicode type. Nothing should be stored internally as an encoded string. If a third party library raises exceptions with strings in an encoding other than ASCII, they should be shot :) JE ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On Fri, May 16, 2014, Igor Kalnitsky ikalnit...@mirantis.com wrote: unicode(exc) (or six.text_type(exc)) works for all exceptions, built-in or custom. That's too much of a statement. Sometimes exceptions implement their own __str__ / __unitcode__ methods, that return too many rubbish information or not enough. What do you do in that case? I don't understand the problem. What are you expecting from unicode(exc)? What exceptions don't meet that expectation? Using str(exc) or unicode(exc) is the standard Python convention to get a useful string out of an exception. This is what the traceback module does (at least in Python 2.7) for the last line in the traceback output. It tries str first, then unicode if str fails. If it uses unicode, then it backslash escapes it back to ASCII. The behavior to try str first and then unicode second appears to be because of legacy reasons. I'm not entirely sure why it backslash escapes unicode back to ASCII. Maybe to avoid a possible second exception when printing it? JE ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
*@Christian,* According to http://legacy.python.org/dev/peps/pep-0352/ the message attribute of BaseException is deprecated since Python 2.6 and was dropped in Python 3.0. Some projects have custom exception hierarchy, with strictly defined attributes (e.g. message, or something else). In a previous mail, I mean exactly that case, not the case with a built-in exceptions. *@Joshua, * Any reason we would want to do this vs using exc_info=True? 1. It will print traceback and it's not behavior we always want. 2. It doesn't handle unicode. I mean, we may have something like this in our output: \u043f\u0440\u0438\u0432\u0435\u0442 On Fri, May 16, 2014 at 5:19 AM, Joshua Harlow harlo...@yahoo-inc.comwrote: Any reason we would want to do this vs using exc_info=True? This leaves it up to the underlying logger to dump the exception, the traceback and whatever else. For example u can do. LOG.warn(Something broke at this point, exc_info=True) From: Igor Kalnitsky ikalnit...@mirantis.com Reply-To: OpenStack Development Mailing List (not for usage questions) openstack-dev@lists.openstack.org Date: Thursday, May 15, 2014 at 2:20 PM To: OpenStack Development Mailing List (not for usage questions) openstack-dev@lists.openstack.org Subject: Re: [openstack-dev] [oslo] Logging exceptions and Python 3 Hi, Is there a reason to log the exception as Unicode on Python 2? Sure, why not? Some exceptions may have unicode message with non-ASCII characters. and we obviously can't cast such exceptions with str() in Python 2.x. The problem is that I don't know what is the best syntax to log exceptions. I hate this unicode dances too, since I don't understand all nuances and potential pitfalls. But I belive the better approach is to use unicode() for Python 2.x and str() for Python 3.x. Example: LOG.error(six.text_type(e)) P.S: I've a quick look over logging implementaion, and figured out that it has some code to dial with unicode. In few words, if we know about exception's attributes, it's better to use it directly to log: Example: LOG.error(e.message) - Igor On Thu, May 15, 2014 at 6:29 PM, Victor Stinner victor.stin...@enovance.com wrote: Hi, I'm trying to define some rules to port OpenStack code to Python 3. I just added a section in the Port Python 2 code to Python 3 about formatting exceptions and the logging module: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions The problem is that I don't know what is the best syntax to log exceptions. Some projects convert the exception to Unicode, others use str(). I also saw six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message contains a non-ASCII character). IMO the safest option is to use str(exc). For example, use LOG.debug(str(exc)). Is there a reason to log the exception as Unicode on Python 2? Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On Fri, May 16, 2014, Igor Kalnitsky ikalnit...@mirantis.com wrote: According to http://legacy.python.org/dev/peps/pep-0352/ the message attribute of BaseException is deprecated since Python 2.6 and was dropped in Python 3.0. Some projects have custom exception hierarchy, with strictly defined attributes (e.g. message, or something else). In a previous mail, I mean exactly that case, not the case with a built-in exceptions. That's a fragile assumption to make. unicode(exc) (or six.text_type(exc)) works for all exceptions, built-in or custom. I don't see the reason why it's being avoided. JE ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On Thu, May 15, 2014, Victor Stinner victor.stin...@enovance.com wrote: I'm trying to define some rules to port OpenStack code to Python 3. I just added a section in the Port Python 2 code to Python 3 about formatting exceptions and the logging module: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions The problem is that I don't know what is the best syntax to log exceptions. Some projects convert the exception to Unicode, others use str(). I also saw six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message contains a non-ASCII character). IMO the safest option is to use str(exc). For example, use LOG.debug(str(exc)). Is there a reason to log the exception as Unicode on Python 2? Because the exception uses unicode characters? This isn't common, but it does happen and a lot of code in nova uses unicode(exc) as a result. Using str(exc) is bad because it will fail with any exception with unicode characters. six.u(exc) is bad too since it's only for text literals. It's mostly obsolete too since Python 3.3 has the u prefix again and I don't think any OpenStack projects target 3.0-3.2 six.text_type(exc) is the recommended solution. JE ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
Le vendredi 16 mai 2014, 06:03:53 Johannes Erdfelt a écrit : On Fri, May 16, 2014, Igor Kalnitsky ikalnit...@mirantis.com wrote: According to http://legacy.python.org/dev/peps/pep-0352/ the message attribute of BaseException is deprecated since Python 2.6 and was dropped in Python 3.0. Some projects have custom exception hierarchy, with strictly defined attributes (e.g. message, or something else). In a previous mail, I mean exactly that case, not the case with a built-in exceptions. That's a fragile assumption to make. unicode(exc) (or six.text_type(exc)) works for all exceptions, built-in or custom. I don't see the reason why it's being avoided. See my documentation: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions six.text_type(exc): always use Unicode. It may raise unicode error depending on the exception, be careful. Example of such error in python 2: unicode(Exception(nonascii:\xe9)). unicode(exc) works with such exception classes: --- class MyException1(Exception): pass exc = MyException1() exc.message = u\u20ac unicode(exc) #ok class MyException2(Exception): def __unicode__(self): return u\20ac exc = MyException2() unicode(exc) #ok --- If we want to format an exception as Unicode, we need a function trying unicode(), or use str() and then guess the encoding. It means adding a new safe function to Olso to format an exception. Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
Another idea, Django has the following: http://bit.ly/1n1cuWm It seems like a useful similar function we can have to do the correct thing for exceptions and other objects. Or maybe we should talk with the django folks to see why they have a useful method like that. -Josh -Original Message- From: Victor Stinner victor.stin...@enovance.com Organization: eNovance Reply-To: OpenStack Development Mailing List (not for usage questions) openstack-dev@lists.openstack.org Date: Friday, May 16, 2014 at 7:04 AM To: OpenStack Development Mailing List (not for usage questions) openstack-dev@lists.openstack.org Subject: Re: [openstack-dev] [oslo] Logging exceptions and Python 3 Le vendredi 16 mai 2014, 06:03:53 Johannes Erdfelt a écrit : On Fri, May 16, 2014, Igor Kalnitsky ikalnit...@mirantis.com wrote: According to http://legacy.python.org/dev/peps/pep-0352/ the message attribute of BaseException is deprecated since Python 2.6 and was dropped in Python 3.0. Some projects have custom exception hierarchy, with strictly defined attributes (e.g. message, or something else). In a previous mail, I mean exactly that case, not the case with a built-in exceptions. That's a fragile assumption to make. unicode(exc) (or six.text_type(exc)) works for all exceptions, built-in or custom. I don't see the reason why it's being avoided. See my documentation: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptio ns six.text_type(exc): always use Unicode. It may raise unicode error depending on the exception, be careful. Example of such error in python 2: unicode(Exception(nonascii:\xe9)). unicode(exc) works with such exception classes: --- class MyException1(Exception): pass exc = MyException1() exc.message = u\u20ac unicode(exc) #ok class MyException2(Exception): def __unicode__(self): return u\20ac exc = MyException2() unicode(exc) #ok --- If we want to format an exception as Unicode, we need a function trying unicode(), or use str() and then guess the encoding. It means adding a new safe function to Olso to format an exception. Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
Hi, Is there a reason to log the exception as Unicode on Python 2? Sure, why not? Some exceptions may have unicode message with non-ASCII characters. and we obviously can't cast such exceptions with str() in Python 2.x. The problem is that I don't know what is the best syntax to log exceptions. I hate this unicode dances too, since I don't understand all nuances and potential pitfalls. But I belive the better approach is to use unicode() for Python 2.x and str() for Python 3.x. Example: LOG.error(six.text_type(e)) P.S: I've a quick look over logging implementaion, and figured out that it has some code to dial with unicode. In few words, if we know about exception's attributes, it's better to use it directly to log: Example: LOG.error(e.message) - Igor On Thu, May 15, 2014 at 6:29 PM, Victor Stinner victor.stin...@enovance.com wrote: Hi, I'm trying to define some rules to port OpenStack code to Python 3. I just added a section in the Port Python 2 code to Python 3 about formatting exceptions and the logging module: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions The problem is that I don't know what is the best syntax to log exceptions. Some projects convert the exception to Unicode, others use str(). I also saw six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message contains a non-ASCII character). IMO the safest option is to use str(exc). For example, use LOG.debug(str(exc)). Is there a reason to log the exception as Unicode on Python 2? Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
On 05/15/2014 11:20 PM, Igor Kalnitsky wrote: Example: LOG.error(e.message) According to http://legacy.python.org/dev/peps/pep-0352/ the message attribute of BaseException is deprecated since Python 2.6 and was dropped in Python 3.0. Christian. -- Christian Berendt Cloud Computing Solution Architect Mail: bere...@b1-systems.de B1 Systems GmbH Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537 ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
Re: [openstack-dev] [oslo] Logging exceptions and Python 3
Any reason we would want to do this vs using exc_info=True? This leaves it up to the underlying logger to dump the exception, the traceback and whatever else. For example u can do. LOG.warn(Something broke at this point, exc_info=True) From: Igor Kalnitsky ikalnit...@mirantis.commailto:ikalnit...@mirantis.com Reply-To: OpenStack Development Mailing List (not for usage questions) openstack-dev@lists.openstack.orgmailto:openstack-dev@lists.openstack.org Date: Thursday, May 15, 2014 at 2:20 PM To: OpenStack Development Mailing List (not for usage questions) openstack-dev@lists.openstack.orgmailto:openstack-dev@lists.openstack.org Subject: Re: [openstack-dev] [oslo] Logging exceptions and Python 3 Hi, Is there a reason to log the exception as Unicode on Python 2? Sure, why not? Some exceptions may have unicode message with non-ASCII characters. and we obviously can't cast such exceptions with str() in Python 2.x. The problem is that I don't know what is the best syntax to log exceptions. I hate this unicode dances too, since I don't understand all nuances and potential pitfalls. But I belive the better approach is to use unicode() for Python 2.x and str() for Python 3.x. Example: LOG.error(six.text_type(e)) P.S: I've a quick look over logging implementaion, and figured out that it has some code to dial with unicode. In few words, if we know about exception's attributes, it's better to use it directly to log: Example: LOG.error(e.message) - Igor On Thu, May 15, 2014 at 6:29 PM, Victor Stinner victor.stin...@enovance.commailto:victor.stin...@enovance.com wrote: Hi, I'm trying to define some rules to port OpenStack code to Python 3. I just added a section in the Port Python 2 code to Python 3 about formatting exceptions and the logging module: https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions The problem is that I don't know what is the best syntax to log exceptions. Some projects convert the exception to Unicode, others use str(). I also saw six.u(str(exc)) which is wrong IMO (it can raise unicode error if the message contains a non-ASCII character). IMO the safest option is to use str(exc). For example, use LOG.debug(str(exc)). Is there a reason to log the exception as Unicode on Python 2? Victor ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.orgmailto:OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev ___ OpenStack-dev mailing list OpenStack-dev@lists.openstack.org http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev