複数の API を呼び出す非常に長時間実行される Python コードがあります。
また、各ステップでロガーを実装して、例外を特定しました。ただし、FileHandler は、Python スクリプトの完了時にのみデータを表示します。
一定間隔でロガーをフラッシュする方法はありますか? ジョブのステータスを監視するために使用できます。
# when executed standalone, the main block is called
if __name__ == '__main__':
# Crate logger for RR api application
logger = logging.getLogger('RR_API')
logger.setLevel(logging.INFO)
# create file handler which logs messages
# If the handler is already defined, then no need to set it again
if not logger.handlers:
fh = logging.FileHandler("C:///Users///2128///python_data_logger.log")
fh.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
#add the handlers to the logger
logger.addHandler(fh)
run_ts = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logger.info('Starting Time' + '|' + run_ts)
print ('Starting Time',datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
count = 0
token = get_temp_token(run_ts)
portfolio = get_orgs(token,'"https://apidata.ratings.com/v1.0/orgs/"',run_ts)
ログファイルへの出力:
2016-12-30 16:22:01,448 - RR_API - INFO - Starting Time|2016-12-30 16:22:01
2016-12-30 16:22:01,526 - RR_API - CRITICAL - No Access Token was downloaded | 2016-12-30 16:22:01
上記の行は、スクリプトの実行が終了したときにのみログ ファイルに表示されます。