我有以下Python代码来记录活动
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y_%m_%d_%H_%M_%S')
today = datetime.datetime.now().strftime('%Y_%m_%d')
log_file = os.path.join(os.path.dirname(os.getcwd()), 'logs', today, 'refresh_'+timestamp+'.log')
logging.basicConfig(level=logging.INFO,
filename=log_file,
format='%(asctime)s %(name).4s %(process)6d/%(threadName)s: %(levelname).4s %(message)s')
def func_1(args):
if abc:
logging.info('--- Refreshing ['+fn+'] completed ---')
except:
logging.critical('--- Refreshing ['+fn+'] failed! ---')
else:
logging.critical('--- Retrieving ['+fn+'] data failed! Error:' + str(page.status_code) + ": " + account + ": " + password + '---')
return;
def func_2(file_path):
logging.info('Loaded all connecting metadata successfully')
return;
if __name__ == '__main__':
func_2(abc)
pool = Pool(processes=threads)
pool.map(func_1, abc)
pool.close()
pool.join()
logging.info('--- Whole process completed ---')
logging.shutdown()
问题是该进程创建了2个具有不同时间的日志文件(因为我在文件名中使用了时间戳到秒)。一个日志文件log_1
记录了func_1
中的所有活动,并首先生成了。另一个log_2
已从代码末尾的Loaded all connecting metadata successfully
和func_2
记录--- Whole process completed ---
;它最后生成了。
我怀疑这是因为有2个处理程序,每个处理程序在不同的时间生成。如何确保将所有活动记录到一个日志文件而不是2?