This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Kronuz
Recipients Kronuz, docs@python, ezio.melotti
Date 2013-02-07.18:18:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1360261105.58.0.464994704341.issue17155@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2013-02-07 18:18:25Kronuzsetrecipients: + Kronuz, ezio.melotti, docs@python
2013-02-07 18:18:25Kronuzsetmessageid: <1360261105.58.0.464994704341.issue17155@psf.upfronthosting.co.za>
2013-02-07 18:18:25Kronuzlinkissue17155 messages
2013-02-07 18:18:25Kronuzcreate