python - Rotate TimedRotatingFileHandler on first calendar day of each month - Stack Overflow

Logging handler TimedRotatingFileHandler supports at most weekly rotation (e.g. W0 for Sunday to Monday

Logging handler TimedRotatingFileHandler supports at most weekly rotation (e.g. W0 for Sunday to Monday transition).

How could I rotate file monthly, e.g. on the first calendar day each month?

I thought about midnight rotation with file renaming only on the first calendar day of month, using ternary logic rename if first day else rename to base , implemented as

import datetime as dt
import logging
from logging.handlers import TimedRotatingFileHandler
    
logger = logging.getLogger("process")
handler = TimedRotatingFileHandler("process.log", when="midnight")
handler.namer = lambda _: (dt.datetime.now() - dt.timedelta(days=1)).strftime("%Y%m_process.log") if dt.datetime.now().day == 1 else "process.log"
logger.addHandler(handler)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744785933a4593625.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信