log.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: jürgen.pannosch, tanja.zolotareva
  4. """
  5. import sys
  6. import os
  7. import logging
  8. from datetime import datetime
  9. sys.path.append(os.getcwd())
  10. class Log():
  11. '''
  12. '''
  13. pass
  14. def __init__(self, name: str = None,
  15. log_file: str = None,
  16. log_level: str = "ERROR",
  17. stdout_log_level: str = "INFO",
  18. print_to_stdout: bool = True):
  19. """Sets the log level and the path where the log file is stored
  20. :param log_file: Path to the log file.
  21. :param log_level: Log level."""
  22. if name is None:
  23. name = ''
  24. self._logger = logging.getLogger(name)
  25. self._logger.setLevel("DEBUG")
  26. if (self._logger.hasHandlers()):
  27. self._logger.handlers.clear()
  28. if log_file is None:
  29. log_file = os.path.join(".", "logs", str(datetime.today().date()) + ".log")
  30. assert(isinstance(log_file, str)),\
  31. "Parameter 'log_path' must be of string type"
  32. os.makedirs(os.path.dirname(log_file), exist_ok=True)
  33. formatter = logging.Formatter(
  34. '\n %(name)s %(asctime)s %(levelname)s %(message)s')
  35. file_handler = logging.FileHandler(log_file)
  36. file_handler.setFormatter(formatter)
  37. file_handler.setLevel(log_level)
  38. self._logger.addHandler(file_handler)
  39. if print_to_stdout:
  40. stream_handler = logging.StreamHandler(sys.stdout)
  41. stream_handler.setFormatter(formatter)
  42. stream_handler.setLevel(stdout_log_level)
  43. self._logger.addHandler(stream_handler)
  44. # self._logger.setLevel(log_level)
  45. @property
  46. def magenta(self):
  47. return '\033[95m'
  48. @property
  49. def blue(self):
  50. return '\033[94m'
  51. @property
  52. def cyan(self):
  53. return '\u001b[36m'
  54. @property
  55. def green(self):
  56. return '\033[92m'
  57. @property
  58. def yellow(self):
  59. return '\033[93m'
  60. @property
  61. def fail(self):
  62. return '\033[91m'
  63. @property
  64. def reset(self):
  65. return '\033[0m'
  66. @property
  67. def bold(self):
  68. return '\033[1m'
  69. @property
  70. def underline(self):
  71. return '\033[4m'
  72. def info(self, message: str):
  73. self._logger.info(message)
  74. def warning(self, message: str):
  75. self._logger.warning(message)
  76. def error(self, message: str):
  77. self._logger.error(message)
  78. def log_and_raise_error(self, message):
  79. '''
  80. '''
  81. self._logger.error(message, exc_info=True)
  82. raise Exception(message)
  83. def log_and_raise_error_stack_info(self, message):
  84. '''
  85. '''
  86. self._logger.error(message, exc_info=True, stack_info=True)
  87. raise Exception(message)
  88. def log_and_raise_warning(self, message):
  89. '''
  90. '''
  91. self._logger.warning(message)
  92. raise Warning(message)