python / python/cpython

Logs are getting auto deleted after the date is changed.

未关闭
#130,108 12 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

pending stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

I have written a code that would rotate logs on midnight maintaing backup of 1 day. But the next day, the previous logs are getting deleted.

For example :
While I am checking logs on 14th Feb, I noticed that all the logs generated on 13th Feb, got deleted from instancelogs.log.2025-02-13. The first log generated on 14th Feb is getting stored in instancelogs.log.2025-02-13 file instead of instancelogs.log file , the remaining logs for 14th Feb are correctly stored in instancelogs.log file.

Please look into the issue as we are losing all the logs for 13th of Feb.

I am using below code to handle logs:



import logging
from logging.handlers import TimedRotatingFileHandler


#logger instances for PPT creator
LOG_FILE_PATH = "../STORE_LOGS/instancelogs.log"

instance_logger = logging.getLogger('instance_logger')

instance_logger_file_handler = None


instance_logger_file_handler = TimedRotatingFileHandler(LOG_FILE_PATH, when='midnight', interval=1, backupCount=1)
instance_logger_file_formatter = logging.Formatter(
    fmt="[%(asctime)s] - [%(levelname)s]  - [%(filename)s] - [%(funcName)s] - %(message)s",
    datefmt="%d-%m-%y %H:%M:%S")
instance_logger_file_handler.setFormatter(instance_logger_file_formatter)

instance_logger.setLevel(logging.DEBUG)
instance_logger.addHandler(instance_logger_file_handler)

CPython versions tested on:

3.9

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Python logging.handlers.TimedRotatingFileHandler 的实现及其 rollover 行为入手,使用提供的午夜配置作为复现用例。检查午夜之后的第一条记录如何被分配到轮转后的文件名,以及 backupCount=1 如何删除文件。当此前一天的日志保持完整,且新一天的记录写入活动日志文件,并为此用例提供回归覆盖时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。