''' func.py '''
import logging
def func():
''' func '''
logging.basicConfig(
format="\t %(filename)s"
" %(funcName)s"
"-ln%(lineno)d"
" %(levelname)s \n%(message)s",
level=logging.DEBUG,
# stream=sys.stdout,
)
logger = logging.getLogger()
logger.info(" >>> logger func info")
logger.warning(" >>> logger func warning")
print(" >>> print func info")
def test_func():
''' test func '''
# caplog.set_level(logging.DEBUG)
func()
if __name__ == "__main__":
test_func()
Run Code Online (Sandbox Code Playgroud)
假设我将上面的代码另存为func.py。当我跑步
pytest -s func.py
Run Code Online (Sandbox Code Playgroud)
我得到
" >>> print func info".
Run Code Online (Sandbox Code Playgroud)
我如何获得
" >>> logger func info"
Run Code Online (Sandbox Code Playgroud)
和
“ >>>记录器功能警告”
当我跑步时
pytest -s func.py
Run Code Online (Sandbox Code Playgroud)
我希望能够这样做的原因如下。我通常在我的代码中插入logging.debug / …