我有一个Python脚本,该脚本使用Requests模块将请求发送到我们的API,然后接收ansver进行解析,并说一切正常。结果显示为“打印”。这是简单的示例:
for i in response:
if i in response_keys:
print('[Request_name] '+i+' is ok')
else:
print('[Request_name] Extra key ' +i+' in resronse)
现在,我需要记录这些打印ti文件。当我像这样使用Logging模块时:
#Example without sending a request, just logging configuration and check
import logging
logging.basicConfig(filename='Autotests_log.log', level=logging.INFO, format='%(asctime)s %(message)s' )
for i in response:
if i in response_keys:
logging.info('[Request_name] '+i+' is ok')
else:
logging.info('[Request_name] '+i+' is not ok')
我在日志文件中看到了这一点:
2016-04-20 14:24:44,823 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,873 [Request_name] success is ok
2016-04-20 14:24:44,874 [Request_name] statusCode is ok
2016-04-20 14:24:44,874 [Request_name] data is ok
2016-04-20 14:24:44,876 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,924 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,974 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:45,017 Starting new HTTP connection (1): 192.168.1.44
我应该怎么做才能在日志中看到这个:
2016-04-20 14:24:44,873 [Request_name] success is ok
2016-04-20 14:24:44,874 [Request_name] statusCode is ok
2016-04-20 14:24:44,874 [Request_name] data is ok
尝试配置requests
日志记录级别
import logging
logging.getLogger("requests").setLevel(logging.WARNING)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句