def read_configuration(self, cfg_file): """ Read the configuration from the argument file :param cfgfile: filename :return: None """ try: path_utils = PathUtils() config = ConfigParser() confirmation = config.read(cfg_file) if len(confirmation) == 0: sys.exit(u'Configuration file not found. Please execute with the configuration file next to the executable file.') _logfile = config.has_option('Logs','Filename') and config.get('Logs','Filename') or './log/application.log' _logfile_max_bytes = int(config.has_option('Logs','MaxBytes') and config.get('Logs','MaxBytes') or 5242880) log_level = config.has_option('Logs','Level') and config.get('Logs','Level') or 'INFO' log_console_option = config.has_option('Logs','Console') and config.get('Logs','Console') or 'FALSE' self.log_console = log_console_option.strip().upper() == 'TRUE' if log_level == 'INFO': log_level = logging.INFO if log_level == 'DEBUG': log_level = logging.DEBUG if log_level == 'WARNING': log_level = logging.WARNING if log_level == 'CRITICAL': log_level = logging.CRITICAL if log_level == 'ERROR': log_level = logging.ERROR if log_level == 'FATAL': log_level = logging.FATAL self.get_config_logger(_logfile, log_level, _logfile_max_bytes, self.log_console) num_task = int(config.has_option('Task', 'Number') and config.get('Task', 'Number') or 0) for index in range(0, num_task): try: name = config.has_option('Task', str(index)) and config.get('Task', str(index)) or None if name is not None: brokers_str = config.has_option(name, 'Brokers') and config.get(name, 'Brokers') or '' brokers = brokers_str.split(',') topic = config.has_option(name, 'Topic') and config.get(name, 'Topic') or '' retries = int(config.has_option(name, 'Retries') and config.get(name, 'Retries') or 0) partitions = config.has_option(name, 'Partitions') and config.get(name, 'Partitions') or [0] key = config.has_option(name, 'Key') and config.get(name, 'Key') or '' value = config.has_option(name, 'Value') and config.get(name, 'Value') or '' locale = config.has_option(name, 'Locale') and config.get(name, 'Locale') or 'En_US' pause_milliseconds = config.has_option(name, 'Pause_milliseconds') and config.get(name, 'Pause_milliseconds') or 0 kafka_task = KafkaTask(name, locale, brokers, topic, retries, partitions, key, value, pause_milliseconds) self.tasks.append(kafka_task) except Exception as e: self.logger.error(u'Error found at Kafka task listed at {0} on the configuration file!'.format(index)) pass except Exception as e: if self.logger is not None: self.logger.error(u'ReadConfiguration Exception {0}.'.format(e)) sys.exit(u'ReadConfiguration Exception {0}.'.format(e))
def get_config_logger(self, logfile, level=logging.INFO, max_bytes=5242880, log_console=False): """ Setup the logger of the receiver. :param logfile: filename of the log :param level: level of verbose :param max_bytes: max bytes of the log file :param log_console: if the log should also printed on the console :return: """ PathUtils().ensure_has_paths(logfile, is_filename=True) self.logger = logging.getLogger('GREGOR') formatter = logging.Formatter(u'%(asctime)s - %(name)s - %(levelname)s - %(message)s') self.logger.setLevel(level) # Console if log_console: ch = logging.StreamHandler(sys.stdout) ch.setFormatter(formatter) self.logger.addHandler(ch) # File fh = handlers.RotatingFileHandler(logfile, maxBytes=max_bytes, backupCount=100, encoding='utf8') fh.setFormatter(formatter) self.logger.addHandler(fh)