美文网首页
python日志定时进行转存

python日志定时进行转存

作者: mengkaidi | 来源:发表于2019-05-20 15:15 被阅读0次

python日志定时进行转存可以借助于 TimedRotatingFileHandler 类,详情可以参考python 的官方文档:

import logging
from logging.handlers import TimedRotatingFileHandler

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

#每分钟进行一次转存
handler = TimedRotatingFileHandler(filename="log", when='M', interval=1)
handler.suffix = "%Y%m%d-%H:%M"
handler.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

handler.setFormatter(formatter)

logger.addHandler(handler)

#测试
while True:
    logger.info('info message')
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')

相关文章

网友评论

      本文标题:python日志定时进行转存

      本文链接:https://www.haomeiwen.com/subject/wuukzqtx.html