Beispiel #1
0
    def test_if_all_logging_children_and_levels_are_reported(self):
        # Validate logging children are reported correctly

        log_1_level = Logger.INFO
        root_level = Logger.DEBUG

        # Set up the loggers
        log_1 = Logger(default_level=log_1_level, test_name='log_1')
        log_2 = Logger(test_name='log_2')
        root = Logger(default_level=root_level, set_root=True)

        # Record the expected names and levels
        expected_names = {log_1.name, log_2.name, root.name}
        expected_log_levels = {log_1.name: root_level,
                               log_2.name: root_level,
                               root.name: root_level}

        # Get the actual logger info
        children = root._list_loggers()
        actual_child_names = set([name for name, level in children])
        actual_log_levels = dict([(name, level) for name, level in children])

        # Print state (for debugging on failure)
        print(root.list_loggers())
        print(f"\nACTUAL NAMES: {actual_child_names}")
        print(f"\nEXPECTED NAMES: {expected_names}")
        print(f"\nINTERSECTION: {actual_child_names.intersection(expected_names)}")

        # The length of the set intersection should be the same length as
        # the expected names.
        assert_equals(len(actual_child_names.intersection(expected_names)),
                      len(expected_names))

        # Check the logging level for the actual level matches the
        # expected level.
        for name, actual_level in actual_log_levels.items():
            expected_level = expected_log_levels.get(name, None)

            if expected_level is not None:
                expected_level = Logger.VAL_TO_STR[expected_level].upper()
                print(f"\nName: {name}\nExpected Value: {expected_level}"
                      f"\nActual Value: {actual_level}")

                assert_equals(actual_level, expected_level)
Beispiel #2
0
    def configure_logging(cls, app_cfg_obj: PdlConfig) -> Logger:
        """
        Sets up the root logger (format, location) and updates the existing the handlers

        :param app_cfg_obj: (PdlConfig): Contains the required location, etc., for the logger

        :return: Instantiated, updated root logger

        """
        # Set log level (CLI overrides the config file)
        # ---------------------------------------------
        # Get the config-file specified value
        log_level = app_cfg_obj.app_cfg.get(
            AppCfgFileSections.LOGGING,
            AppCfgFileSectionKeys.LOG_LEVEL.lower())

        if app_cfg_obj.cli_args.debug:
            log_level = 'debug'

        # Determine and store the log file name, create directory if needed.
        log_dir = os.path.abspath(
            os.path.sep.join(
                app_cfg_obj.logfile_name.split(os.path.sep)[0:-1]))
        utils.check_if_location_exists(location=log_dir, create_dir=True)

        # Setup the root logger for the app
        logger = Logger(filename=app_cfg_obj.logfile_name,
                        default_level=Logger.STR_TO_VAL[log_level],
                        project=app_cfg_obj.app_cfg.get(
                            AppCfgFileSections.PROJECT,
                            AppCfgFileSectionKeys.NAME),
                        set_root=True)

        # Show defined loggers and log levels
        logger.debug(f"Log File: {app_cfg_obj.logfile_name}")
        for line in logger.list_loggers().split('\n'):
            logger.debug(line)

        return logger