How to encode log file?

encoding, logging, python, python-3.x

Solution

`basicConfig` does not take an `encoding` argument, but in Python 3.3 you can do this instead:

logging.basicConfig(handlers=[logging.FileHandler(nombreFicheroLog, 'w', 'utf-8')], 
                    level=logging.DEBUG)

For older Pythons, see this question.

Problem

My code: ``` logging.warning('FASE VALIDACIÓN TITULOS DE COLUMNAS DE DATOS NO SUPERADA. compruebe los nombres de los títulos de las columnas en datos.csv)') ``` Output in .log file: ``` WARNING:root:FASE VALIDACI�N TITULOS DE COLUMNAS DE DATOS NO SUPERADA. compruebe los nombres de los t�tulos de las columnas en datos.csv) ``` Then I tried this: ``` logging.basicConfig(filename=nombreFicheroLog, encoding="utf-8", level=logging.DEBUG) ``` But it does not work. Then I tried this one: ``` logging.warning(u'FASE VALIDACIÓN TITULOS DE COLUMNAS DE DATOS NO SUPERADA. compruebe los nombres de los títulos de las columnas en datos.csv)') ``` But the output is the same. How I can encode the .log file to support UTF-8? P.S. I'm using Python3.

Original source

Related problems