日志记录不为第二天创建文件,而是在旧日期日志文件中附加信息
logging not creating file for next day instead appending info in old date log file
我正在尝试使用日志记录模块维护日志,但是一旦我启动脚本,它就会继续追加到当前日期的日志文件中,而不是为第二天创建新文件。脚本是 运行 24 hrs.I 我不确定我该如何处理。
我的代码:
from datetime import date, datetime
import time
import logging
LogDirectory="D:\Python_Collateral_Validation_Service\Logs"
while True:
logfile=LogDirectory+"\"+str(date.today().strftime("%d/%m/%Y")).replace("/", "")+".log"
print("log file name := ",logfile)
logging.basicConfig(filename = logfile, level=logging.INFO, format='%(asctime)s %(message)s')
logging.info("CollateralValidation-> CSV File Not Present")
time.sleep(90)
打印语句:
log file name := D:\Python_Collateral_Validation_Service\Logs012022.log
log file name := D:\Python_Collateral_Validation_Service\Logs012022.log
PS D:\Alok\CR06645_PythonCode_phase2>
日志文件仅为 06012022 生成,但它也在为 07012022 写入。预期的 07012022.log 未生成。
06012022.log
的输出
2022-01-06 16:11:53,480 CollateralValidation-> CSV File Not Present
2022-01-06 16:12:26,580 CollateralValidation-> CSV File Not Present
2022-01-06 16:15:00,987 CollateralValidation-> CSV File Not Present
2022-01-07 16:16:29,998 CollateralValidation-> CSV File Not Present
07012022 未在最后一行上方生成
删除 while True :
并在任务计划程序上定期将脚本安排到 运行。有一个选项可以做到这一点。
为什么我喜欢这种方法:
- 记录的文件名在每个 运行 评估,因此您始终可以获得当天的正确文件名。脚本不需要复杂的逻辑来处理日期更改。
- 如果您正确安排任务(运行 在计算机启动选项),您将不必担心 运行在计算机启动时运行脚本。
- 长 运行ning 脚本比 运行 执行其工作并定期停止的脚本更容易崩溃/行为笨拙
这不是一个纯粹的 python 解决方案,但它可以可靠地工作。
最后,提个建议:使用“%Y%m%d”作为您的日志文件名格式。在浏览日志文件时,这将允许更好的文件排序。
This function does nothing if the root logger already has handlers configured
在同一句话中有一个建议的解决方案
unless the keyword argument force is set to True.
我正在尝试使用日志记录模块维护日志,但是一旦我启动脚本,它就会继续追加到当前日期的日志文件中,而不是为第二天创建新文件。脚本是 运行 24 hrs.I 我不确定我该如何处理。
我的代码:
from datetime import date, datetime
import time
import logging
LogDirectory="D:\Python_Collateral_Validation_Service\Logs"
while True:
logfile=LogDirectory+"\"+str(date.today().strftime("%d/%m/%Y")).replace("/", "")+".log"
print("log file name := ",logfile)
logging.basicConfig(filename = logfile, level=logging.INFO, format='%(asctime)s %(message)s')
logging.info("CollateralValidation-> CSV File Not Present")
time.sleep(90)
打印语句:
log file name := D:\Python_Collateral_Validation_Service\Logs012022.log
log file name := D:\Python_Collateral_Validation_Service\Logs012022.log
PS D:\Alok\CR06645_PythonCode_phase2>
日志文件仅为 06012022 生成,但它也在为 07012022 写入。预期的 07012022.log 未生成。
06012022.log
的输出2022-01-06 16:11:53,480 CollateralValidation-> CSV File Not Present
2022-01-06 16:12:26,580 CollateralValidation-> CSV File Not Present
2022-01-06 16:15:00,987 CollateralValidation-> CSV File Not Present
2022-01-07 16:16:29,998 CollateralValidation-> CSV File Not Present
07012022 未在最后一行上方生成
删除 while True :
并在任务计划程序上定期将脚本安排到 运行。有一个选项可以做到这一点。
为什么我喜欢这种方法:
- 记录的文件名在每个 运行 评估,因此您始终可以获得当天的正确文件名。脚本不需要复杂的逻辑来处理日期更改。
- 如果您正确安排任务(运行 在计算机启动选项),您将不必担心 运行在计算机启动时运行脚本。
- 长 运行ning 脚本比 运行 执行其工作并定期停止的脚本更容易崩溃/行为笨拙
这不是一个纯粹的 python 解决方案,但它可以可靠地工作。
最后,提个建议:使用“%Y%m%d”作为您的日志文件名格式。在浏览日志文件时,这将允许更好的文件排序。
This function does nothing if the root logger already has handlers configured
在同一句话中有一个建议的解决方案
unless the keyword argument force is set to True.