logger.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from abc import ABC, abstractmethod
  2. class logger(ABC):
  3. def __init__(self):
  4. pass
  5. @abstractmethod
  6. def info(self, content):
  7. pass
  8. @abstractmethod
  9. def warning(self, content):
  10. pass
  11. @abstractmethod
  12. def error(self, content):
  13. pass
  14. class stdio_logger(logger):
  15. def info(self, content):
  16. print("[INFO]: " + str(content))
  17. def warning(self, content):
  18. print("[WARNING]: " + str(content))
  19. def error(self, content):
  20. print("[ERROR]: " + str(content))
  21. class dummy_logger(logger):
  22. def info(self, content):
  23. pass
  24. def warning(self, content):
  25. pass
  26. def error(self, content):
  27. pass
  28. class file_logger(logger):
  29. def __init__(self, file):
  30. try:
  31. self.file = open(file, "w")
  32. except:
  33. print("Can not open log file: \"" + file + "\"")
  34. exit(-1)
  35. def __del__(self):
  36. self.file.close()
  37. def info(self, content):
  38. try:
  39. self.file.write("[INFO]: " + str(content) + "\n")
  40. except:
  41. print("Can not write to log file!")
  42. exit(-1)
  43. def warning(self, content):
  44. try:
  45. self.file.write("[WARNING]: " + str(content) + "\n")
  46. except:
  47. print("Can not write to log file!")
  48. exit(-1)
  49. def error(self, content):
  50. try:
  51. self.file.write("[ERROR]: " + str(content) + "\n")
  52. except:
  53. print("Can not write to log file!")
  54. exit(-1)
  55. class loader:
  56. _logger = None
  57. def get():
  58. if loader._logger is None:
  59. Exception("Logger bust be set in loader first.")
  60. return loader._logger
  61. def set(new_logger):
  62. if type(new_logger) is not logger:
  63. Exception("New loader must be instance of logger.")
  64. loader._logger = new_logger
  65. def __init__(self):
  66. Exception("Loader is static class.")