logging module and binary strings

Frank Aune Frank.Aune at broadpark.no
Wed Jul 1 11:02:20 EDT 2009


Hello,

---- snip ----
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> x='\xfe\x9f\xce\xc3\xa1\x00\xff\x01'
>>> x
'\xfe\x9f\xce\xc3\xa1\x00\xff\x01'
>>> print x
���á�
>>> logging.info(x)
Traceback (most recent call last):
  File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit
    stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 10: 
ordinal not in range(128)
>>> logging.info(x, extra={'encoding':'utf-8'})
Traceback (most recent call last):
  File "/usr/lib/python2.6/logging/__init__.py", line 773, in emit
    stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'utf8' codec can't decode byte 0xfe in position 10: 
unexpected code byte

---- snip ----

Is there any way to log the above value of x "properly" using the python 
logging module? By properly I mean logging the value displayed when entering 
just x in the python shell and pressing enter. 

Regards,
Frank





More information about the Python-list mailing list