New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue in unicode args in logging #42451
Comments
logging has an issue in handling unicode object >>> import logging
>>>
>>> class Obj:
... def __init__(self,name):
... self.name = name
... def __str__(self):
... return self.name
...
>>> # a non-ascii string
...
>>> obj = Obj(u'\u00f6')
>>>
>>> # this will cause error
...
>>> print '%s' % obj
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode
character u'\xf6' in position 0: ordinal not in range(128)
>>>
>>> # this will promote to unicode (and the console also
happen to be able to display it)
...
>>> print u'%s' % obj
ö
>>>
>>> # this works fine
... # (other than logging makes its own decision to
encode in utf8)
...
>>> logging.error(u'%s' % obj)
ERROR:root:b
>>>
>>> # THIS IS AN UNEXPECTED PROBLEM!!!
...
>>> logging.error(u'%s', obj)
Traceback (most recent call last):
File "C:\Python24\lib\logging\__init__.py", line 706, in
emit
msg = self.format(record)
File "C:\Python24\lib\logging\__init__.py", line 592, in
format
return fmt.format(record)
File "C:\Python24\lib\logging\__init__.py", line 382, in
format
record.message = record.getMessage()
File "C:\Python24\lib\logging\__init__.py", line 253, in
getMessage
msg = msg % self.args
UnicodeEncodeError: 'ascii' codec can't encode
character u'\xf6' in position 0: ordinal not in range(128)
>>>
>>> # workaround the str() conversion in getMessage()
...
>>> logging.error(u'%s-\u00f6', obj)
ERROR:root:b-b The issue seems to be in LogRecord.getMessage(). It msg = str(self.msg) I am not sure why ti want to do the conversion. The last |
Logged In: YES Unassinging the bug. I don't know anything about the logging Hint: perhaps the logging module should grow an .encoding |
Logged In: YES Vinay, any suggestions? |
Logged In: YES Misc. changes were backported into Python 2.4.2, please The problem is not with msg = str(self.msg) but rather with msg = msg % args To ensure good Unicode support, ensure your messages are msg = msg % args should result in a Unicode object. You can pass this to a The encoding additions were made in Revision 1.26 of Marking as closed. |
Logged In: YES
That's what I am doing already. Let me explain the subtle problem again.
I can understand how 1 fails. But I expect 2,3 and 4 to work |
Logged In: YES Aaah...now I understand! Sorry for being a little slow, and I've uploaded a fix to CVS: str(msg) is only called if msg import logging
class X:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def main():
obj = X(u'\u00f6')
logging.error(u'%s' % obj)
logging.error(u'%s', obj)
logging.error(u'%s-\u00f6', obj)
if __name__ == "__main__":
main()
# Now gives the following output on my system (default ERROR:root:Â |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: