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条)