| Total Complexity | 3 |
| Total Lines | 40 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """ |
||
| 2 | LoggingNeeds |
||
| 3 | |||
| 4 | This library sets all required parameters for a rotating LOG file |
||
| 5 | """ |
||
| 6 | from datetime import datetime |
||
| 7 | # package for Log management |
||
| 8 | import logging |
||
| 9 | import logging.handlers as handlers |
||
| 10 | |||
| 11 | |||
| 12 | class LoggingNeeds: |
||
| 13 | logger = None |
||
| 14 | |||
| 15 | def initiate_logger(self, logger_file_name, logger_internal_name): |
||
| 16 | # initiate Logging |
||
| 17 | self.logger = logging.getLogger(logger_internal_name) |
||
| 18 | # set logging level to desired level only if specified |
||
| 19 | if logger_file_name == 'None': |
||
| 20 | self.logger.setLevel(logging.NOTSET) |
||
| 21 | else: |
||
| 22 | self.logger.setLevel(logging.DEBUG) |
||
| 23 | # defining log file and setting rotating logic |
||
| 24 | log_handler = handlers.TimedRotatingFileHandler(logger_file_name, |
||
| 25 | when='h', |
||
| 26 | interval=1, |
||
| 27 | backupCount=5, |
||
| 28 | encoding='utf-8', |
||
| 29 | utc=True) |
||
| 30 | # ensure timestamps are reported as UTC time-zone |
||
| 31 | my_converter = lambda x, y: datetime.utcnow().timetuple() |
||
| 32 | logging.Formatter.converter = my_converter |
||
| 33 | # Here we define our formatter |
||
| 34 | log_formatter = logging.Formatter( |
||
| 35 | '%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
||
| 36 | # Here we set our logHandler's formatter |
||
| 37 | log_handler.setFormatter(log_formatter) |
||
| 38 | # pairing the handler with logging |
||
| 39 | self.logger.addHandler(log_handler) |
||
| 40 |