import logging
import time
import re
from logging.handlers import TimedRotatingFileHandler
from logging.handlers import RotatingFileHandler
 
def main():
 #日志打印格式
 log_fmt = '%(asctime)s\tFile \"%(filename)s\",line %(lineno)s\t%(levelname)s: %(message)s'
 formatter = logging.Formatter(log_fmt)
 #创建TimedRotatingFileHandler对象
 log_file_handler = TimedRotatingFileHandler(filename="ds_update", when="D", interval=1, backupCount=30)
 #log_file_handler.suffix = "%Y-%m-%d_%H-%M.log"
 #log_file_handler.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}.log$")
 log_file_handler.setFormatter(formatter) 
 logging.basicConfig(level=logging.INFO)
 log = logging.getLogger()
 log.addHandler(log_file_handler)
 #循环打印日志
 log_content = "test log"
 count = 0
 while count < 600:
  log.error(log_content)
  time.sleep(1)
  count = count + 1
 log.removeHandler(log_file_handler)
 
 
if __name__ == "__main__":
 main()
