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

标签 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

最佳答案

您需要通过 --log-format 传递与 log_cli_format 中指定的相同的格式字符串,或者在您的文件中设置 log_format 选项pytest.ini/pyproject.tomlpyproject.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"

关于python - Pytest 日志记录忽略捕获日志的 pytest.ini log_cli_format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67425295/

相关文章:

MySQL 根据登录/注销条目查询总在线时间

Java 日志接口(interface)

python - 当 pytest.raises 失败时,将自定义消息附加到输出的最佳方法是什么?

python - 如何使用 ctypes 打包和解包(结构 <-> str)

python - 当我运行我的函数时,我不断收到此错误 :- SyntaxError: unexpected EOF while parsing

python - 父子进程之间的通信

python - 如何测试 Connexion/Flask 应用程序?

Python将多个单词列表转换为单个单词

python - 将 python 日志记录格式应用于包含新行的消息的每一行

python - 如何使用 PyTest 正确测试 Django API ListView?