ThreadPoolExecutor来完成任务。
我还想在其中实现日志记录。我只想将特定的调试、信息或警告语句写入日志文件中。但实际上它会将每个请求都写入日志文件。
如何让它只写入我用logging.info或logging.warning等指定的特定语句到文件中。
以下是我的代码片段:
logging.basicConfig(filename='BOM.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('Logger initiated')
with ThreadPoolExecutor(max_workers=100) as executor:
startt = time.time()
futures = [executor.submit(get_movie_details, movie_id) for movie_id in all_movies_ids]
for result in as_completed(futures):
all_movies_summary_data.append(result)
endt = time.time()
print("Time Taken: {:.6f}s".format(endt - startt))
以下是日志文件的样子:
2019-03-31 16:21:04,722 - DEBUG - Logger initiated
2019-03-31 16:21:04,731 - DEBUG - Starting new HTTPS connection (1): www.boxofficemojo.com:443
2019-03-31 16:21:04,733 - DEBUG - Starting new HTTPS connection (2): www.boxofficemojo.com:443
2019-03-31 16:21:04,736 - DEBUG - Starting new HTTPS connection (3): www.boxofficemojo.com:443
.
.
.
我该如何确保只获取记录在日志文件中的Logger,而不是其他内容。尽管我没有明确地说明要记录那些项,为什么日志文件中会出现额外的内容。
我是否完全错误地理解了日志记录或其他事情?
我尝试按照glhr在其中一个答案中建议的设置日志级别。
但它的输出结果如下所示。
2019-03-31 17:07:29,817 - INFO - Logger initiated
2019-03-31 17:07:30,981 - WARNING - Connection pool is full, discarding connection: www.boxofficemojo.com
2019-03-31 17:07:30,994 - WARNING - Connection pool is full, discarding connection: www.boxofficemojo.com
2019-03-31 17:07:30,997 - WARNING - Connection pool is full, discarding connection: www.boxofficemojo.com