我想知道我执行的接口测试用例中,接口发送的请求是否正确,所以我想把 requests 发送的内容加入日志里面,
但一直只能在控制台输出相关信息,不能把请求写到日志文件里面

import requests
import logging
try:
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client
a=http_client.HTTPConnection.debuglevel = 1
logger = logging.getLogger("requests.packages.urllib3")
file_handler = logging.FileHandler(r'C:\Users\ason\PycharmProjects\pythonProject\Zsk_Auto_API\Test_Data\log.txt',
                                   "a+", "UTF-8")
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
# logger.propagate = True
logger.addHandler(file_handler)
logger.debug(requests.get("https://www.codenong.com/16337511/"))


↙↙↙阅读原文可查看相关链接,并与作者交流