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@python
components: Documentation, Unicode
messages: 181640
nosy: Kronuz, docs@python, ezio.melotti
priority: normal
severity: normal
status: open
title: Logging throwing UnicodeEncodeError exception
versions: Python 2.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue17155>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to