python logging module is not writing anything to file

Put this

for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

in front of the

logging.basicConfig(...)

see also Logging module not writing to file


I know that this question might be a bit too old but I found the above method a bit of an overkill. I ran into a similar issue, I was able to solve it by:

import logging

logging.basicConfig(format = '%(asctime)s %(message)s',
                    datefmt = '%m/%d/%Y %I:%M:%S %p',
                    filename = 'example.log',
                    level=logging.DEBUG)

This will write to example.log all logs that are of level debug or higher.

logging.debug("This is a debug message") will write This is a debug message to example.log. Level is important for this to work.


Try to put the import and the basicConfig at the very beggining of the script. Something like this:

import logging
logging.basicConfig(filename='log.log', level=logging.INFO)
.
.
import ...
import ...

Try calling

logger.error('This should go to both console and file')

instead of

logging.error('this will go to the default logger which you have not changed the config of')