简体   繁体   English

有没有办法自定义从 conftest.py 登录 pytest 的格式

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

Am new to pytest and am trying to setup logging我是 pytest 的新手,正在尝试设置日志记录

I have the following code in my conftest.py file我的 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)

Another file logger.py另一个文件 logger.py

Log = logging.getLogger(log_fname)

on writing Log.info("message")关于写Log.info("message")

I see the log written as我看到日志写成
00:16.748 inf logfile_00_00_13_10_2020.log: message 00:16.748 inf logfile_00_00_13_10_2020.log:消息

Not sure how or where this format is defined.不确定如何或在哪里定义此格式。

Can we customise it to a format like我们可以将其自定义为类似的格式吗

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

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

Where should be the right place to define this?应该在哪里定义它?

In the pytest.ini file:pytest.ini文件中:

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

You also don't need to add your own log configuration directly into test code, just enable pytest's live logging features (documented here ).您也不需要将自己的日志配置直接添加到测试代码中,只需启用 pytest 的实时日志记录功能(在此处记录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM