簡體   English   中英

有沒有辦法自定義從 conftest.py 登錄 pytest 的格式

[英]Is there a way to customise the format for logging in pytest from conftest.py

我是 pytest 的新手,正在嘗試設置日志記錄

我的 conftest.py 文件中有以下代碼

def pytest_logger_config(logger_config):
    logger_config.add_loggers([log_fname], stdout_level=logging.INFO)
    logger_config.set_log_option_default(log_fname)

另一個文件 logger.py

Log = logging.getLogger(log_fname)

關於寫Log.info("message")

我看到日志寫成
00:16.748 inf logfile_00_00_13_10_2020.log:消息

不確定如何或在哪里定義此格式。

我們可以將其自定義為類似的格式嗎

log_file_format = %(levelname)s: %(asctime)s (%(filename)s:%(lineno)s %(message)s)

信息:2020-10-13 00:00:45 (.py:<line_no>) 消息

應該在哪里定義它?

pytest.ini文件中:

[pytest]
log_format =  %(levelname)s: %(asctime)s (%(filename)s:%(lineno)s %(message)s)
log_date_format = %Y-%m-%d %H:%M:%S

您也不需要將自己的日志配置直接添加到測試代碼中,只需啟用 pytest 的實時日志記錄功能(在此處記錄)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM