如何在Fabric中使用Python logging
模块?
我已在fabfile.py
中设置了一些测试并配置了记录器等,但由于它使用local()
执行,我猜测日志永远不会被设置在测试或功能模块调用中。
这是我的fabfile.py
:
from fabric.api import local
import logging
import module
def unittest(name='all'):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
fh = logging.FileHandler('logs/unittest.log')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - ' +
'%(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('Unittest started.')
local('py.test module')
然后在我的模块中,module.py
import pytest
import logging
def test_main():
logger = getLogger(__name__)
logger.info('Function started.')
日志中包含来自fabfile.py
但来自module.py
答案 0 :(得分:2)
您的module.py在此行中有错误:
logger = getLogger(__name__)
应该是:
logger = logging.getLogger(__name__)
毕竟你是对的:module.py中的记录器独立于fabfile中的记录器。如果您在pytest会话中为测试用例设置了记录器,那么您就可以了。