Using Python 2.6 (don't judge me ;-) ) with the logging module, I'm finding that trying to log an entire dict with unicode bits is causing problems.
log.debug("mydict is %s", mydict)
This is causing UnicodeDecodeError exceptions when run in a test runner, but not when I'm in a simple python shell.
>>> d
{'foo': u'\u2615'}
>>> d['bar'] = d['foo'].encode('utf8')
>>> d
{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}
>>> print d['foo']
☕
>>> print d['bar']
☕
>>>
>>> d
{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}
>>> print "%s" % d
{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}
>>> import logging
>>> logging.basicConfig()
>>> log = logging.getLogger('')
>>> log.setLevel(logging.DEBUG)
>>> log.debug("%s", d)
DEBUG:root:{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}
So in a terminal it's all good, but in a test runner I'm getting exceptions as python is apparently trying to decode the repr string of the dict using an ascii codec.
So, I'm wondering why the inconsistency? And what is a good, safe way to send a dict to the logger for debug purposes?
Cheers, Mike