I'm trying to create a utf-8 log file with Python 2.7 on Win 8. I'm always getting UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 20: ordinal not in range(128) when I'm trying to log Windows paths that contain non-latin characters in them. The following setup invariably throws the dreaded UnicodeDecodeError:
import logging
from logging import handlers
def GetLogger():
logger = logging.getLogger('log')
if not len(logger.handlers):
logger.setLevel(logging.DEBUG)
logger.propagate = False
# create a file handler
fileHandler = logging.handlers.TimedRotatingFileHandler('mylog.log', when = 'midnight', backupCount = 10, encoding = 'utf-8')
fileHandler.setLevel(logging.DEBUG)
# unicode template here
fileHandler.setFormatter(logging.Formatter(u'[%(levelname)-8s: %(asctime)s - %(filename)s:%(lineno)s - %(funcName)s()] - %(message)s'))
logger.addHandler(fileHandler)
return logger
logger = GetLogger()
path = 'e:\\\xcf\xf0\xee' + 'foo.bar' # actually I'm just calling os.getcwd() + 'foo.bar' here, and that's what it returns
print type(path) # prints <type 'str'>
logger.warning('Didn\'t find path %s' % path) # <- POOF!
# logger.warning('Didn\'t find path %s' % 'abcde') <- This would work
I tried wrapping path in unicode(path), but it didn't help. I'm at my wit's end. How do I log these tricky paths?
path = u'e:\\\xcf\xf0\xee' + u'foo.bar'?