Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest:如何将日志重定向到控制台和使用tmp_路径创建的tmp文件_Python_Logging_Pytest_Fixtures - Fatal编程技术网

Python Pytest:如何将日志重定向到控制台和使用tmp_路径创建的tmp文件

Python Pytest:如何将日志重定向到控制台和使用tmp_路径创建的tmp文件,python,logging,pytest,fixtures,Python,Logging,Pytest,Fixtures,我正在使用pytest框架编写一个脚本,其中需要将日志重定向到控制台以及使用fixture(tmp_路径)创建的临时文件。我编写了以下代码,正在创建文件,但未观察控制台日志或文件中的日志: import os import logging from datetime import datetime global LOG_FILENAME script_name = os.path.splitext(os.path.basename(__file__))[0] LOG_FILENAME = d

我正在使用pytest框架编写一个脚本,其中需要将日志重定向到控制台以及使用fixture(tmp_路径)创建的临时文件。我编写了以下代码,正在创建文件,但未观察控制台日志或文件中的日志:

import os
import logging
from datetime import datetime

global LOG_FILENAME

script_name = os.path.splitext(os.path.basename(__file__))[0]
LOG_FILENAME = datetime.now().strftime(script_name + "_%H_%M_%S_%d_%m_%Y.log")

def test_create_file(tmp_path):
    d = tmp_path / "Logs"
    d.mkdir()
    p = d / LOG_FILENAME

    p.write("content")
    logging.info(p.strpath)
    logging.basicConfig(filename=str(p), level=logging.INFO)
    logging.info(str(p))

    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
    console.setFormatter(formatter)
    logging.getLogger("").addHandler(console)

    logging.info("This is log1 message")
    logging.info("This is log2 message")
    assert p.read_text() == "content"
我希望我的log1和log2消息应该被捕获到我在上面创建的日志文件中,并且在控制台中可见。但这些要求都没有得到满足。 以下是我的输出:

nishantsaha@ztphost:~/home/nishantsaha/tests$ python3 -m pytest -v test_tmpdir.py -s
========================================================== test session starts ==========================================================
platform linux -- Python 3.6.9, pytest-5.1.2, py-1.8.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=<bucket_type>
metadata: {'Python': '3.6.9', 'Platform': 'Linux-4.15.0-76-generic-x86_64-with-Ubuntu-18.04-bionic', 'Packages': {'pytest': '5.1.2', 'py': '1.8.0', 'pluggy': '0.13.1'}, 'Plugins': {'random-order': '1.0.4', 'json-report': '1.2.0', 'metadata': '1.8.0'}}
rootdir: /home/nishantsaha/tests
plugins: random-order-1.0.4, json-report-1.2.0, metadata-1.8.0
collected 1 item

test_tmpdir.py::test_create_file FAILED
nishantsaha@ztphost:~/home/nishantsaha/tests$python3-m pytest-v test\u tmpdir.py-s
========================================================================测试会话开始==========================================================
平台linux——Python 3.6.9、pytest-5.1.2、py-1.8.0、pluggy-0.13.1--/usr/bin/python3
cachedir:.pytest\u缓存
测试顺序随机化未启用。使用--random order或--random order bucket启用=
元数据:{'Python':'3.6.9','Platform':'Linux-4.15.0-76-generic-x86_64-with-Ubuntu-18.04-bionic','Packages':{'pytest':'5.1.2','py':'1.8.0','pluggy':'0.13.1','Plugins':{'random-order':'1.0.4','json report'1.2.0','metadata':'1.8.0'}
rootdir:/home/nishantsaha/tests
插件:random-order-1.0.4、json-report-1.2.0、metadata-1.8.0
收集1项
test_tmpdir.py::test_create_文件失败
有没有关于我遗漏了什么以及如何解决这个问题的建议?如果我使用相同的代码但没有fixture,那么在这种情况下,文件和控制台的日志都会被观察到

您需要将记录器(“”)级别设置为
INFO
或更低,仅设置处理程序级别是不够的

logging.getLogger("").setLevel('INFO')
默认情况下,记录器的级别为
警告
(30)。有效级别是记录器级别和处理程序级别的交叉点,由
getEffectiveLevel()
提供