logging module and binary strings

Peter Otten __peter__ at web.de
Wed Jul 1 19:44:56 CEST 2009


Frank Aune wrote:

>>>> 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'

>>>> 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

> 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.

How about logging.info(repr(x))?

Peter





More information about the Python-list mailing list