Reputation: 1204
I have a utility module (let's call it "uapi.py") which provides me access to an API. My module contains several functions which perform various calls to the API in different ways. I have written a Pytest script for it ("uapi-pytest.py"), which exercises each of the functions, and hence the different calls supported by the API.
Module uapi.py uses the standard 'logging' module to log events. The default config is defined 'globally' within uapi.py, right below the import statements, and is set to a level of WARN. This level is fine when I use the module interactively (through main, which incorporates a CLI). However, I would also like to be able to import the module into my Pytest script and override the default log level to something lower (e.g. INFO or DEBUG), so that if any tests fail I can see the more detailed output, and figure out what went wrong.
So far the only way I've figured out how to do this is by editing uapi.py and setting the level to INFO before each Pytest run, then setting it back afterwards. Since my Pytest script imports uapi.py, I think I should be able to override its default logging behavior, but so far I haven't been able to figure out how. I tried directly setting the module's logging attribute but I end up getting this error (when run as "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 ========================================
Can anyone help me figure this out? Maybe I am missing something obvious, but to be honest, I can't see the forest for the trees with the Python logging module, which seems unnecessarily abstract.
Here is some stripped down code that shows the error I am getting:
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
Upvotes: 4
Views: 3083
Reputation: 107040
Since uapy.py
is using the default Logger
object, which can be obtained by calling logging.getLogger()
, you can set the desired logging level for it by calling its bound method of setLevel
instead:
logging.getLogger().setLevel(logging.INFO)
Upvotes: 4
Reputation: 532208
You are trying to invoke the method on the class, not the instance of Logger
you want to configure.
import pytest
import logging
import uapi as u
# Configure the uapi logger, from which both
# uapi.uapi_get_site_info and uapi.uapi_get_site_version_info
# inherit
logging.getLogger('uapi').setLevel(logging.INFO)
def test_uapi_get_site_info_before_config_change():
...
def test_uapi_get_site_version_info():
...
Upvotes: 3