If I want my application to be bullet-proof against external libraries that can (and often do) raise Exception(u'nonascii'), I cannot use python's logger.exception() directly, as it will end up with UnicodeDecodeError.
The reason is hidden in Formatter.format() which does:
s = self._fmt % record.__dict__
One can use his own formatter that can handle UnicodeDecodeError, but many users are quite unaware of such issue and it would be great if Python can handle it by itself.
Here is a simple case.
>>> e = Exception(u'ą')
>>> logging.basicConfig()
>>> logging.getLogger('general').exception(u'ą')
ą
None
>>> logging.getLogger('general').exception(e)
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 842, in emit
msg = self.format(record)
File "/usr/lib/python2.7/logging/__init__.py", line 719, in format
return fmt.format(record)
File "/usr/lib/python2.7/logging/__init__.py", line 467, in format
s = self._fmt % record.__dict__
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0105' in position 0: ordinal not in range(128)
Logged from file <stdin>, line 1
>>> unicode(e)
u'\u0105'
>>> str(e)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0105' in position 0: ordinal not in range(128)
>>>
|