SHARE
TWEET

Untitled

dmtyan97 Aug 17th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from time import gmtime, strftime
  2.  
  3.  
  4. def get_current_datetime():
  5.     return strftime("%Y-%m-%d %H:%M:%S", gmtime())
  6.  
  7.  
  8. class MyLogger:
  9.  
  10.     def __init__(self, text_file_name='sample', level = 'DEBUG'):
  11.         self.text_file_name = text_file_name
  12.         self.log_file = open(text_file_name+'.log', 'a')
  13.         self.config_logger(level=level)
  14.         self.format = format
  15.  
  16.  
  17.     def config_logger(self, text_file_name='', level='DEBUG'):
  18.         if text_file_name != '' and text_file_name != self.text_file_name:
  19.             self.log_file.close()
  20.             self.text_file_name = text_file_name
  21.             self.log_file = open(text_file_name + '.log', 'a')
  22.  
  23.         if level.upper() == 'DEBUG': self.level = 'DEBUG'
  24.         elif level.upper() == 'INFO': self.level = 'INFO'
  25.         elif level.upper() == 'WARNING' or level.upper() == 'WARN': self.level = 'WARNING'
  26.         elif level.upper() == 'ERROR': self.level = 'ERROR'
  27.         elif level.upper() == 'CRITICAL' or level.upper() == 'FATAL': self.level = 'CRITICAL'
  28.         elif level == '': pass
  29.         else: raise Exception('Incorrect Level')
  30.  
  31.     def compare_levels(self, level):
  32.         if level.upper() == 'CRITICAL' or level.upper() == 'FATAL':
  33.             return True
  34.         elif level.upper() == 'ERROR':
  35.             if self.level == 'CRITICAL':
  36.                 return False
  37.             return True
  38.         elif level.upper() == 'WARNING' or level.upper() == 'WARN':
  39.             if self.level == 'CRITICAL' or self.level == 'ERROR':
  40.                 return False
  41.             return True
  42.         elif level.upper() == 'INFO':
  43.             if self.level == 'INFO' or self.level == 'DEBUG':
  44.                 return True
  45.             else:
  46.                 return False
  47.         elif level.upper() == 'DEBUG':
  48.             if self.level == 'DEBUG':
  49.                 return True
  50.             else:
  51.                 return False
  52.  
  53.         else:
  54.             print(level)
  55.             raise Exception('Incorrect Level')
  56.  
  57.     def write_log(self, current_script_name='', text='', *formatargs, mode='file', level='DEBUG'):
  58.         if self.compare_levels(level):
  59.             text = text.format(*formatargs)
  60.             if mode == 'file':
  61.                 self.log_file.write(get_current_datetime() + ':  ' + level + ': ' + current_script_name + ': ' + text + '\n')
  62.                 self.log_file.close()
  63.             elif mode == 'console':
  64.                 print(get_current_datetime() + ':  ' + level + ': ' + current_script_name + ': ' + text + '\n')
  65.             else:
  66.                 self.log_file.write(get_current_datetime() + ':  ' + level + ': ' + current_script_name + ': ' + text + '\n')
  67.                 self.log_file.close()
  68.                 print(get_current_datetime() + ':  ' + level + ': ' + current_script_name + ': ' + text + '\n')
  69.  
  70.  
  71. log = MyLogger()
  72. log.write_log('project1', 'pidrila {} {}', 1, 2, mode='file', level='DEBUG')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top