Advertisement
Guest User

doublepoop

a guest
Dec 5th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | None | 0 0
  1. import time
  2. import datetime
  3.  
  4. class Logger:
  5.  
  6.     __default_time_format = None
  7.     __last_update = None
  8.     __filename = None
  9.     __filemode = None
  10.     __module_logger = None
  11.  
  12.     # Use __logger_configured as decorator for other members. When true, other static methods are unlocked.
  13.     __logger_configured = False
  14.  
  15.     @staticmethod
  16.     def config_logger(filename: str, module_logger: str = __name__, default_filemode = 'a+', default_time_format = '%A %d %B %Y, %H:%M:%S'):
  17.  
  18.         '''
  19.        :param level: Logging level.
  20.        :param filename: The file we want to log to
  21.        :param default_filemode: The filemode we will choose to log as
  22.        :param default_time_format: The datetime format all we will use when logging
  23.        :return: None
  24.        '''
  25.  
  26.         Logger.__default_time_format = default_time_format
  27.         Logger.__last_update = datetime.datetime.utcnow().strftime(Logger.__default_time_format)
  28.         Logger.__filemode = default_filemode
  29.         Logger.__filename = filename
  30.         Logger.__module_logger = module_logger.upper()
  31.  
  32.         if not isinstance(Logger.__filename, str):
  33.             raise ValueError('[{} -- ERROR] {} -- Filename must be a string. Invalid filename: {}'.format(Logger.__module_logger, Logger.__last_update, Logger.__filename))
  34.  
  35.         # Logger now configured. Other static member functions are unlocked.
  36.         Logger.__logger_configured = True
  37.  
  38.         print('[SUCCESS] {} -- Logger configured. Current settings: '
  39.               'default time formatting: {}, '
  40.               'filename: {}, '
  41.               'filemode: {}, '
  42.               'module name: {}'
  43.               .format(Logger.__last_update, Logger.__default_time_format, Logger.__filename, Logger.__filemode, Logger.__module_logger))
  44.  
  45.  
  46.     ###########################
  47.     ####### Decorators ########
  48.     ###########################
  49.  
  50.     # We don't want checkLoggerStatus to be a member function of Logger. Hence
  51.     # Hence, make it a staticmethod of a private nested class.
  52.     class _Decorators:
  53.  
  54.         @staticmethod
  55.         def check_logger_status(func):
  56.             def wrapper(*args, **kwargs):
  57.                 if not True:
  58.                     raise ValueError('[ERROR] -- The Logger has not yet been configured. '
  59.                                      'Please first configure the logger via its config_logger staticmethod')
  60.                 else:
  61.                     func(*args, **kwargs)
  62.             return wrapper
  63.  
  64.         @staticmethod
  65.         def check_status(logger_status: bool):
  66.             if logger_status:
  67.                 pass
  68.             else:
  69.                 raise ValueError('[ERROR] -- The Logger has not yet been configured. '
  70.                                  'Please first configure the logger via its config_logger staticmethod')
  71.  
  72.  
  73.     ##########################
  74.     ####### Misc Funcs #######
  75.     ##########################
  76.  
  77.     def __update_time():
  78.         # Method called, update last time Logger did something
  79.         Logger.__last_update = datetime.datetime.utcnow().strftime(Logger.__default_time_format)
  80.  
  81.     def __output_log_message(operation: str) -> str:
  82.         # Print to screen
  83.         print(operation, end='')
  84.         return operation
  85.  
  86.     ##########################
  87.     ### Logging Facilities ###
  88.     ##########################
  89.  
  90.     @staticmethod
  91.     #@_Decorators.check_logger_status
  92.     def log_info(operation: str):
  93.  
  94.         '''
  95.        :param operation: State what you are doing so that the logger can write it to file.
  96.                          For example, Logger.log_info('Connected to API successfully!')
  97.        :return None
  98.        '''
  99.  
  100.         Logger.__update_time()
  101.         our_message = Logger.__output_log_message('[{} -- INFO] {} -- {}\n'.format(Logger.__module_logger, Logger.__last_update, operation))
  102.  
  103.         # Write to file
  104.         with open(Logger.__filename, Logger.__filemode) as ourFile:
  105.             ourFile.write(our_message)
  106.  
  107.  
  108.     @staticmethod
  109.     #@_Decorators.check_logger_status
  110.     def log_critical(operation: str):
  111.  
  112.         Logger.__update_time()
  113.         our_message = Logger.__output_log_message('[{} -- CRITICAL] {} -- {}\n'.format(Logger.__module_logger, Logger.__last_update, operation))
  114.  
  115.         # Write to file
  116.         with open(Logger.__filename, Logger.__filemode) as ourFile:
  117.             ourFile.write(our_message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement