Message181640
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. |
|
Date |
User |
Action |
Args |
2013-02-07 18:18:25 | Kronuz | set | recipients:
+ Kronuz, ezio.melotti, docs@python |
2013-02-07 18:18:25 | Kronuz | set | messageid: <1360261105.58.0.464994704341.issue17155@psf.upfronthosting.co.za> |
2013-02-07 18:18:25 | Kronuz | link | issue17155 messages |
2013-02-07 18:18:25 | Kronuz | create | |
|