gpt4 book ai didi

python - 使用 pytest 记录毫秒数

转载 作者:行者123 更新时间:2023-12-04 08:56:44 31 4
gpt4 key购买 nike

目标
pytest 的日志输出上显示毫秒.
我尝试过的事情
pytest 文档伪装成 use time.strftime 用于日志记录的兼容字符串,所以我不能使用 %f格式为微秒,如 datetime.strftimepytest.ini

[pytest]
log_cli = True
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_level = DEBUG
#log_cli_date_format = %H:%M:%S:%f
test_this.py
import logging

def test_me():
logging.info('something')
$ python3 -m pytest
==== test session starts ====
[...]

test_this.py::test_me
----- live log call -----
19:18:10 INFO something
PASSED [100%]

=== 1 passed in 0.01s ===
我不明白的 pytest log_cli_format documents

Sets a logging-compatible string used to format live logging messages.


但是,默认情况下 logging utility日志 %(asctime)s以毫秒为单位

Human-readable time when the LogRecord was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time).



是否有任何简单的解决方案来显示毫秒?日志包默认的方式是什么?它必须是hacky吗?

最佳答案

即使 pytest 覆盖日志记录的默认日期格式,额外的 %(msecs)d来自 LogRecords attributes似乎正是针对这种问题。pytest.ini

[pytest]
log_cli = True
log_cli_format = %(asctime)s.%(msecs)03d %(levelname)s %(message)s
log_cli_level = DEBUG
输出
$ python3 -m pytest
==== test session starts ====
[...]

test_this.py::test_me
----- live log call -----
19:56:57.249 INFO something
PASSED [100%]

=== 1 passed in 0.01s ===

关于python - 使用 pytest 记录毫秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63782079/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com