logger.py 735 B

12345678910111213141516171819
  1. import logging
  2. class Logger():
  3. def __init__(self, path="log.txt"):
  4. self.logger = logging.getLogger("Logger")
  5. self.file_handler = logging.FileHandler(path, "w")
  6. self.stdout_handler = logging.StreamHandler()
  7. self.logger.addHandler(self.file_handler)
  8. self.logger.addHandler(self.stdout_handler)
  9. self.stdout_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
  10. self.file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
  11. self.logger.setLevel(logging.INFO)
  12. def info(self, txt):
  13. self.logger.info(txt)
  14. def close(self):
  15. self.file_handler.close()
  16. self.stdout_handler.close()