Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于长时间运行python代码的记录器-我可以使用它来监视执行状态吗_Python_Logging_Filehandler - Fatal编程技术网

用于长时间运行python代码的记录器-我可以使用它来监视执行状态吗

用于长时间运行python代码的记录器-我可以使用它来监视执行状态吗,python,logging,filehandler,Python,Logging,Filehandler,我有一个很长时间运行的python代码,它调用多个api 我还在每一步都实现了logger来识别异常。但是,FileHandler仅在python脚本完成时显示数据 有没有办法在固定时间间隔后刷新记录器?我可以用它来监控工作的状态 # when executed standalone, the main block is called if __name__ == '__main__': # Crate logger for RR api application log

我有一个很长时间运行的python代码,它调用多个api

我还在每一步都实现了logger来识别异常。但是,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

只有在脚本执行结束时,以上几行才在日志文件中可见。

您是否在脚本结束时等待了一段时间,以确保在执行脚本之前确实刷新了日志?我甚至没有看到您在脚本的输出中显示的“关键”消息。也许你的脚本太小了,在你意识到日志行被写入文件之前退出得很快?谢谢你的回复。关键消息是从get_temp_token(run_ts)调用传递的。这是为了获取剩余REST调用的访问令牌。