예제 #1
0
 def test_by_nonsense_is_err(self):
     with pytest.raises(ValueError) as err:
         with tempfile.NamedTemporaryFile() as log:
             de_logger.set_logging(log_level='INFO',
                                   max_backup_count=6,
                                   file_rotate_by="nonsense",
                                   max_file_size=1000000,
                                   log_file_name=log.name)
     assert "Incorrect 'file_rotate_by'" in str(err.value)
예제 #2
0
def test_by_time(log_setup):
    with tempfile.NamedTemporaryFile() as log:
        log.flush()
        de_logger.set_logging(log_level="INFO",
                              rotation_interval=1,
                              file_rotate_by="time",
                              rotation_time_unit="D",
                              log_file_name=log.name)

        assert log_setup.hasHandlers() is True
        assert "TimedRotatingFileHandler" in str(log_setup.handlers)
        assert log_setup.debug("debug") is None
        assert log_setup.info("infomsg") is None
예제 #3
0
def test_by_size(log_setup):
    with tempfile.NamedTemporaryFile() as log:
        log.flush()
        de_logger.set_logging(log_level="INFO",
                              max_backup_count=6,
                              file_rotate_by="size",
                              max_file_size=1000000,
                              log_file_name=log.name)

        assert log_setup.hasHandlers() is True
        assert "RotatingFileHandler" in str(log_setup.handlers)
        assert log_setup.debug("debug") is None
        assert log_setup.info("infomsg") is None
예제 #4
0
def _make_logger(global_config):
    if 'logger' not in global_config:
        raise RuntimeError("No logger configuration has been specified.")
    try:
        logger_config = global_config['logger']
        de_logger.set_logging(
            log_file_name=logger_config['log_file'],
            max_file_size=logger_config['max_file_size'],
            max_backup_count=logger_config['max_backup_count'],
            log_level=logger_config.get('log_level', 'WARNING'))
        return de_logger.get_logger()
    except Exception as msg:
        raise RuntimeError(f"Failed to create log: {msg}")
def _make_de_logger(global_config):
    if 'logger' not in global_config:
        raise RuntimeError("No logger configuration has been specified.")
    try:
        logger_config = global_config['logger']
        de_logger.set_logging(log_level=logger_config.get('log_level', 'INFO'),
                              file_rotate_by=logger_config.get('file_rotate_by', "size"),
                              rotation_time_unit=logger_config.get('rotation_time_unit', 'D'),
                              rotation_interval=logger_config.get('rotation_time_interval', 1),
                              max_backup_count=logger_config.get('max_backup_count', 6),
                              max_file_size=logger_config.get('max_file_size', 1000000),
                              log_file_name=logger_config['log_file'])
        return de_logger.get_logger()
    except Exception as msg:  # pragma: no cover
        raise RuntimeError(f"Failed to create log: {msg}")
예제 #6
0
    def test_by_time(self):
        with tempfile.NamedTemporaryFile() as log:
            mylogger = de_logger.set_logging(log_level='INFO',
                                             rotation_interval=1,
                                             file_rotate_by="time",
                                             rotation_time_unit='D',
                                             log_file_name=log.name)

            assert mylogger.hasHandlers() is True
            assert 'TimedRotatingFileHandler' in str(mylogger.handlers)
            de_logger.set_stream_logging()
            assert mylogger.debug('debug') is None
            assert mylogger.info('infomsg') is None
예제 #7
0
    def test_by_size(self):
        with tempfile.NamedTemporaryFile() as log:
            mylogger = de_logger.set_logging(log_level='INFO',
                                             max_backup_count=6,
                                             file_rotate_by="size",
                                             max_file_size=1000000,
                                             log_file_name=log.name)

            assert mylogger.hasHandlers() is True
            assert 'RotatingFileHandler' in str(mylogger.handlers)
            de_logger.set_stream_logging()
            assert mylogger.debug('debug') is None
            assert mylogger.info('infomsg') is None
예제 #8
0
                self.channel.publishers[action].worker.publish(data_block)


if __name__ == '__main__':
    import decisionengine.framework.dataspace.dataspace as dataspace
    import os
    import string

    config_manager = configmanager.ConfigManager()
    config_manager.load()
    global_config = config_manager.get_global_config()
    print "GLOBAL CONF", global_config

    try:
        de_logger.set_logging(
            log_file_name=global_config['logger']['log_file'],
            max_file_size=global_config['logger']['max_file_size'],
            max_backup_count=global_config['logger']['max_backup_count'])
    except Exception, e:
        print e
        sys.exit(1)

    my_logger = logging.getLogger("decision_engine")
    my_logger.info("Starting decision engine")

    if len(sys.argv) > 1:
        channel_name = sys.argv[1]
        channel_conf = os.path.join(config_manager.channel_config_dir,
                                    channel_name)
        with open(os.path.abspath(channel_conf), "r") as f:
            channels = {}
            channel_name = channel_name.split('.')[0]
    def load(self):
        self.last_update_time = os.stat(self.config_file).st_mtime
        code = None
        try:
            with open(self.config_file, "r") as f:
                code = "self.global_config=" + "".join(f.readlines())
            if code:
                try:
                    exec(code)
                except Exception as msg:
                    raise RuntimeError("Configuration file {} contains errors: {}".
                                       format(self.config_file, msg))
            else:
                raise RuntimeError(
                    "Empty configuration file {}".format(self.config_file))
        except Exception as msg:
            raise RuntimeError("Failed to read configuration file {} {}".
                               format(self.config_file, msg))

        if not self.logger:
            try:
                logger_config = self.global_config['logger']
                de_logger.set_logging(log_file_name=logger_config['log_file'],
                                      max_file_size=logger_config['max_file_size'],
                                      max_backup_count=logger_config['max_backup_count'])
                self.logger = de_logger.get_logger()
            except Exception as msg:
                raise RuntimeError("Failed to create log: {}".format(msg))

        """
        load channels
        """

        for direntry in os.listdir(self.channel_config_dir):
            if not direntry.endswith(".conf"):
                continue
            name = direntry.split('.')[0]
            channel_conf = os.path.join(self.channel_config_dir, direntry)
            try:
                with open(os.path.abspath(channel_conf), "r") as f:
                    code = "self.channels[name]=" + "".join(f.readlines())
                    try:
                        exec(code)
                    except Exception as msg:
                        self.logger.error("Channel configuration file {} \
                                           contains error {}, SKIPPING".
                                          format(channel_conf, msg))
                        continue
            except Exception as msg:
                self.logger.error("Failed to open channel configuration file {} \
                                  contains error {}, SKIPPING".
                                  format(channel_conf, msg))

            """
            check that channel configuration contains necessary keys
            if keys are missing channel is removed and error is printed
            """
            try:
                self.validate_channel(self.channels[name])
            except Exception as msg:
                self.logger.error(
                    "{} {}, REMOVING the channel".format(name, msg))
                del self.channels[name]
                continue