【问题标题】:Pytest logging ignores pytest.ini log_cli_format for captured logsPytest 日志记录忽略捕获日志的 pytest.ini log_cli_format
【发布时间】:2021-05-06 20:06:02
【问题描述】:

我有一个这样的 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

【问题讨论】:

    标签: python logging pytest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-25
      • 2013-04-22
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多