[New-bugs-announce] [issue17155] Logging throwing UnicodeEncodeError exception
Germán M. Bravo
report at bugs.python.org
Thu Feb 7 19:18:25 CET 2013
New submission from Germán M. Bravo:
I've seen *a lot* of people using `logging.exception(exc)` to log exceptions. It all seems okay, until the exc object contains unicode strings, at which point logging throes a UnicodeEncodeError exception.
Example: `exc = Exception(u'operaci\xf3n'); logger.error(exc)` throws an exception, while `exc = Exception(u'operaci\xf3n'); logger.error(u"%s", exc)` does not and works as expected.
The problem for this is in the `_fmt` string in logging being `"%(message)s"`, not `u"%(message)s"`, which ends up getting the string (non-unicode) version of the exception object (returned by `getMessage()`) and failing to apply the formatting since the exception contains unicode.
A solution would be to make the default formatting string a unicode string so the object returned by `getMessage()` (the exception) is converted to unicode by making all formatting strings for logging unicode strings: (could be done for example by changing to `unicode(self._fmt) % record.__dict__` the line logging/__init__.py:467).
Other solution could be to encourage users not to use objects as the first argument to the logging methods, and perhaps even log a warning against it if it's done.
assignee: docs at python
components: Documentation, Unicode
nosy: Kronuz, docs at python, ezio.melotti
title: Logging throwing UnicodeEncodeError exception
versions: Python 2.7
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce