[openstack-dev] [oslo] Logging exceptions and Python 3

John Dennis jdennis at redhat.com
Wed May 21 17:45:58 UTC 2014


On 05/21/2014 01: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.

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?


-- 
John



More information about the OpenStack-dev mailing list