Pytest 日志记录忽略捕获日志的 pytest.ini log_cli_format

时间:2021-05-06 20:06:02

标签: python logging pytest

我有一个 pytest.ini 文件:

log_cli = 1
log_cli_level = INFO
log_cli_format = [%(levelname)s]: %(message)s
;log_file = ./Logs/pytest_.log
log_file_level = INFO
log_file_format = %(asctime)s [%(levelname)s]: %(message)s
log_file_date_format=%Y-%m-%d %H:%M:%S

在执行时,实时日志和文件日志遵循给定的格式,但也有打印的“捕获的日志”,它们不遵循给定的格式。有什么方法可以修改“捕获的日志”的格式。这是一个示例输出:

------- live log setup ---------
[INFO]: Using local chrome browser....
[INFO]: Successfully logged in
[INFO]: Click Customer Success
[INFO]: The Dashboard Loaded Successfully 


--- Captured log setup ---------
INFO     conftest:conftest.py:46 Using local chrome browser....
INFO     conftest:conftest.py:125 Successfully logged in
INFO     page_objects.CommonPageObjects:CommonPageObjects.py:292 Click Customer Success
INFO     page_objects.CommonPageObjects:CommonPageObjects.py:300 The Dashboard Loaded Successfully

1 个答案:

答案 0 :(得分:1)

您需要通过 --log-format 中指定的 log_cli_format 传递相同的格式字符串,或者在您的 log_format/pytest.ini 中设置 pyproject.toml 选项。 pyproject.toml 的示例:

[tool.pytest.ini_options]
log_cli = true
log_cli_level = "INFO"
log_format = "[%(levelname)s]: %(message)s"
log_cli_format = "[%(levelname)s]: %(message)s"

这同样适用于日期格式,如果您想通过 asctime 关键字包含时间戳。将 log_date_format 设置为与 log_cli_date_format 相同的格式字符串或从命令行通过 --log-date-format 传递它。例如:

log_date_format = "%Y-%m-%d %H:%M:%S"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"