[Twisted-Python] logging and Unicode strings

Manlio Perillo manlio_perillo at libero.it
Sat Jul 14 10:14:11 MDT 2007


It seems that it is not possibile to pass Unicode strings to the logging 
system.


2007/07/14 18:10 CEST [HTTPChannel,0,127.0.0.1] <unicode instance at 
-1254556832 with str error Traceback (most recent call last):

           File 
"/usr/lib/python2.4/site-packages/twisted/python/reflect.py", line 463, 
in safe_str
             return str(o)

         UnicodeEncodeError: 'ascii' codec can't encode character 
u'\xf2' in position 31: ordinal not in range(128)
         >


Since I do not want to encode every string, I have changed safe_str:
def safe_str(o):
     try:
         return str(o)
     except:
         if isinstance(o, unicode):
             import sys
             encoding = sys.getdefaultencoding()
             return o.encode(encoding, 'backslashreplace')

         strExc = '\n'.join(traceback.format_exception(*sys.exc_info()))
         clsName = _determineClassName(o)
         obId = id(o)
         return '<%s instance at %s with str error %s>' % (
             clsName, obId, strExc)


Is this a feature or a bug?



Thanks and regards  Manlio Perillo




More information about the Twisted-Python mailing list