如何覆盖Python模块的默认日志记录级别?

时间:2020-06-01 18:23:37

标签: python logging pytest

我有一个实用程序模块(我们称其为“ uapi.py”),该模块可让我访问API。我的模块包含几个函数,这些函数以不同的方式执行对API的各种调用。我已经为此编写了一个Pytest脚本(“ uapi-pytest.py”),该脚本行使了每个功能,并因此行使了API支持的各种调用。

模块uapi.py使用标准的“日志记录”模块记录事件。默认配置在uapi.py中,在import语句正下方“全局”定义,并设置为WARN级别。当我以交互方式使用模块时(通过集成了CLI的 main ),此级别很好。但是,我也希望能够将该模块导入到我的Pytest脚本中,并将默认日志级别覆盖到较低的级别(例如INFO或DEBUG),这样,如果任何测试失败,我可以看到更详细的输出,并弄清楚出了什么问题。

到目前为止,我想出办法的唯一方法是编辑uapi.py并在每次运行Pytest之前将级别设置为INFO,然后再将其设置回去。由于我的Pytest脚本导入了uapi.py,因此我认为我应该能够覆盖其默认日志记录行为,但是到目前为止,我还无法弄清楚该如何做。我尝试直接设置模块的日志记录属性,但最终遇到此错误(当以“ pytest -v uapi_pytest.py”运行时):

============================================= ERRORS =============================================
________________________________ ERROR collecting uapi_pytest.py _________________________________
uapi_pytest.py:5: in <module>
    u.logging.Logger.setLevel(logging.INFO)
E   TypeError: setLevel() missing 1 required positional argument: 'level'
==================================== short test summary info =====================================
ERROR uapi_pytest.py - TypeError: setLevel() missing 1 required positional argument: 'level'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================== 1 error in 0.64s ========================================

有人可以帮我解决这个问题吗?也许我缺少一些明显的东西,但是老实说,我看不到带有Python日志记录模块的树木的树林,这似乎是不必要的抽象。

以下是一些简化的代码,显示了我遇到的错误:

uapi.py:

import logging
import requests
import fire

logging.basicConfig(level=logging.WARN,
                    format='%(asctime)s %(filename)s %(funcName)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

def uapi_get_site_info(site_id, url, username, password):
    logger = logging.getLogger('uapi.uapi_get_site_info')
    r = requests.post('https://httpbin.org/post', data = {'SiteID': site_id})
    logger.info('GET_SITE_INFO_REQUEST response:%s', r)
    return r

def uapi_get_site_version_info(site_id, version, url, username, password):
    logger = logging.getLogger('uapi.uapi_get_site_version_info')
    r = requests.post('https://httpbin.org/post', data = {'SiteID': site_id, 'Version': version})
    logger.info('GET_SITE_VERSION_INFO_REQUEST response:%s', r)
    return r

def main():
    # Configure the CLI using Python Fire
    fire.Fire({
        'get_info': uapi_get_site_info,
        'version_info': uapi_get_site_version_info,
    })

if __name__ == "__main__":
    main()

uapi_pytest.py:

import pytest
import logging
import uapi as u

u.logging.Logger.setLevel(logging.INFO)

def test_uapi_get_site_info_before_config_change():
    assert True

def test_uapi_get_site_version_info():
    assert True

2 个答案:

答案 0 :(得分:2)

您正在尝试在类上调用方法,而不是要配置的<autocomplete-region-component :query="json_encode(old('regionName'))"> </autocomplete-region-component> 实例。

function App() {
  return (
    <div className="App">
      <header className="App-header">
    <UploadImage/>
    <UploadImage/>
      </header>
    </div>
  );
}

答案 1 :(得分:1)

由于uapy.py使用默认的Logger对象(可以通过调用logging.getLogger()获得),因此可以通过调用其绑定方法{{1 }}:

setLevel