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 tungwaiyip
Recipients
Date 2005-10-05.18:11:46
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
logging has an issue in handling unicode object 
arguments.

>>> 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 
attempts to convert msg to byte string:

   msg = str(self.msg)

I am not sure why ti want to do the conversion. The last 
example workaround this by making sure msg is not 
convertible to byte string.
History
Date User Action Args
2007-08-23 14:35:13adminlinkissue1314107 messages
2007-08-23 14:35:13admincreate