New submission from Piotr Czachur <zim...@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue12339>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to