pythonloggingpywinautopywin

Not writing to log file when using pywinauto


I am trying to automate some Windows action using pywinauto, but when I import pywinauto, logging to the log file stops working.

Before importing - the code is writing the log the file, as in the following example:

import logging

logging.basicConfig(filename='log.txt', filemode='a', level=logging.DEBUG, format="%(message)s",)
logging.info("Test")

.....

After importing - the code is NOT writing the log the file, as in the following example:

import logging
from pywinauto import application

logging.basicConfig(filename='log.txt', filemode='a', level=logging.DEBUG, format="%(message)s",)
logging.info("Test")

.....

Solution

  • Turns out that pywinauto has its own usage of logging module.

    In pywinauto/actionlogger.py, the code sets the logging level to WARNING, which disables writing of log messages under WARNING level (INFO, DEBUG and NOTSET levels) to the log file.

    I have found a workaround to continue working with both pywinauto and logging - just importing pywinauto after the basic configuration of logging, instead of in the beginning:

    import logging
    
    logging.basicConfig(filename='log.txt', filemode='a', level=logging.DEBUG, format="%(message)s",)
    
    from pywinauto import application
    
    logging.info("Test")
    
    .....
    

    This example works well - writes "Test" to the log file.