logging module and binary strings

Robert Kern robert.kern at gmail.com
Wed Jul 1 19:51:31 EDT 2009


On 2009-07-01 10:02, Frank Aune wrote:
> 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.

logging.info(repr(x))

Or, to be more descriptive in your logging message:

logging.info("Got a binary value: %r", x)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list