[New-bugs-announce] [issue12339] logging.Formatter.format() assumes exception to support str() method which is not true for many libraries.

Piotr Czachur report at bugs.python.org
Wed Jun 15 15:42:01 CEST 2011


New submission from Piotr Czachur <zimnyx at gmail.com>:

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)
>>>

----------
components: Library (Lib)
messages: 138369
nosy: Piotr.Czachur
priority: normal
severity: normal
status: open
title: logging.Formatter.format() assumes exception to support str() method which is not true for many libraries.
type: feature request
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12339>
_______________________________________


More information about the New-bugs-announce mailing list