1

我正在尝试使用pywinauto自动执行某些 Windows 操作,但是当我import pywinauto登录到日志文件时,它会停止工作。

导入之前 - 代码正在将日志写入文件,如下例所示:

import logging

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

.....

导入后 - 代码不会将日志写入文件,如下例所示:

import logging
from pywinauto import application

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

.....
4

1 回答 1

3

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.

于 2016-08-16T21:29:16.970 回答