test03.py 928 B

1234567891011121314151617181920212223242526272829
  1. import time
  2. import logging
  3. from logging.handlers import TimedRotatingFileHandler
  4. # 创建一个logger
  5. xp_logger = logging.getLogger('xpLogger')
  6. xp_logger.setLevel(logging.DEBUG) # 设置日志级别
  7. # 创建一个handler,用于写入文件,并根据时间自动轮换日志文件
  8. xp_handler = TimedRotatingFileHandler('log/选品日志', when='midnight', interval=1, backupCount=300)
  9. xp_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))
  10. # 添加handler到logger
  11. xp_logger.addHandler(xp_handler)
  12. def main():
  13. try:
  14. xp_logger.debug('try here')
  15. while True:
  16. xp_logger.debug('while here')
  17. time.sleep(10)
  18. return # 使用 break 来退出循环,而不是 return
  19. except Exception as e:
  20. xp_logger.debug(f'商品发报价信息接口请求--失败: {e}')
  21. finally:
  22. xp_logger.debug('finally')
  23. if __name__ == "__main__":
  24. main()