diff --git a/spinn_utilities/conf_loader.py b/spinn_utilities/conf_loader.py index 94328016..f1391541 100644 --- a/spinn_utilities/conf_loader.py +++ b/spinn_utilities/conf_loader.py @@ -13,7 +13,6 @@ # limitations under the License. -from configparser import NoOptionError import logging import os from typing import Callable, Dict, List, Sequence, Tuple, Union @@ -25,6 +24,7 @@ from spinn_utilities.configs import ( CamelCaseConfigParser, ConfigTemplateException, NoConfigFoundException, UnexpectedConfigException) + logger = log.FormatAdapter(logging.getLogger(__name__)) _SectionParser: TypeAlias = Callable[[CamelCaseConfigParser], None] @@ -98,28 +98,6 @@ def install_cfg_and_error( return NoConfigFoundException(msg) -def logging_parser(config: CamelCaseConfigParser): - """ - Create the root logger with the given level. - - Create filters based on logging levels - - .. note:: - You do not normally need to call this function; it is used - automatically to parse Logging configuration sections. - """ - try: - if config.getboolean("Logging", "instantiate"): - level = config.get("Logging", "default").upper() - logging.basicConfig(level=level) - for handler in logging.root.handlers: - handler.addFilter( - log.ConfiguredFilter(config)) # type: ignore[arg-type] - handler.setFormatter(log.ConfiguredFormatter(config)) - except NoOptionError: - pass - - def _check_config( cfg_file: str, default_configs: CamelCaseConfigParser, strict: bool): """ @@ -251,7 +229,6 @@ def load_config( _read_a_config(configs, cfg_file, default_configs, True) parsers = dict(config_parsers) - parsers["Logging"] = logging_parser for section in parsers: if configs.has_section(section): diff --git a/spinn_utilities/config_holder.py b/spinn_utilities/config_holder.py index 092993d8..b1d39dbf 100644 --- a/spinn_utilities/config_holder.py +++ b/spinn_utilities/config_holder.py @@ -13,13 +13,15 @@ # limitations under the License. from collections import defaultdict +from configparser import NoOptionError import logging import os from typing import Any, Callable, Collection, Dict, List, Optional, Set, Union import spinn_utilities.conf_loader as conf_loader from spinn_utilities.configs import CamelCaseConfigParser from spinn_utilities.exceptions import ConfigException -from spinn_utilities.log import FormatAdapter +from spinn_utilities.log import ( + FormatAdapter, ConfiguredFilter, ConfiguredFormatter) # pylint: disable=global-statement logger = FormatAdapter(logging.getLogger(__file__)) @@ -84,6 +86,27 @@ def _pre_load_config() -> CamelCaseConfigParser: return load_config() +def logging_parser(config: CamelCaseConfigParser): + """ + Create the root logger with the given level. + + Create filters based on logging levels + """ + try: + if (has_config_option("Logging", "instantiate") and + get_config_bool("Logging", "instantiate")): + level = "INFO" + if has_config_option("Logging", "default"): + level = get_config_str("Logging", "default").upper() + logging.basicConfig(level=level) + for handler in logging.root.handlers: + handler.addFilter( + ConfiguredFilter(config)) # type: ignore[arg-type] + handler.setFormatter(ConfiguredFormatter(config)) + except NoOptionError: + pass + + def load_config() -> CamelCaseConfigParser: """ Reads in all the configuration files, resetting all values. @@ -100,6 +123,8 @@ def load_config() -> CamelCaseConfigParser: __config = CamelCaseConfigParser() for default in __default_config_files: __config.read(default) + + logging_parser(__config) return __config diff --git a/spinn_utilities/log.py b/spinn_utilities/log.py index 7658eb6b..26684ada 100644 --- a/spinn_utilities/log.py +++ b/spinn_utilities/log.py @@ -40,7 +40,9 @@ class ConfiguredFilter(object): def __init__(self, conf): self._levels = ConfiguredFormatter.construct_logging_parents(conf) - self._default_level = _LEVELS[conf.get("Logging", "default")] + self._default_level = logging.INFO + if conf.has_option("Logging", "default"): + self._default_level = _LEVELS[conf.get("Logging", "default")] def filter(self, record): """ @@ -63,7 +65,8 @@ class ConfiguredFormatter(logging.Formatter): __last_component = re.compile(r'\.[^.]+$') def __init__(self, conf): - if conf.get("Logging", "default") == "debug": + if (conf.has_option("Logging", "default") and + conf.get("Logging", "default") == "debug"): fmt = "%(asctime)-15s %(levelname)s: %(pathname)s: %(message)s" else: fmt = "%(asctime)-15s %(levelname)s: %(message)s" diff --git a/spinn_utilities/spinn_utilities.cfg b/spinn_utilities/spinn_utilities.cfg index fa892ea3..380ee6af 100644 --- a/spinn_utilities/spinn_utilities.cfg +++ b/spinn_utilities/spinn_utilities.cfg @@ -3,6 +3,10 @@ # Edit the cfg in your home directory to change your preferences # Add / Edit a cfg in the run directory for script specific changes +[Logging] +instantiate = True +default = info + [Machine] machine_spec_file = None diff --git a/unittests/test_cfg_checker.py b/unittests/test_cfg_checker.py index 78ed0f14..c33b0b9e 100644 --- a/unittests/test_cfg_checker.py +++ b/unittests/test_cfg_checker.py @@ -26,4 +26,4 @@ def setUp(self): def test_config_checks(self): spinn_utilities_dir = spinn_utilities.__path__[0] run_config_checks(directories=[spinn_utilities_dir], - exceptions=["config_holder.py"]) + exceptions=[]) diff --git a/unittests/test_log.py b/unittests/test_log.py index aad534f7..1b4bae1d 100644 --- a/unittests/test_log.py +++ b/unittests/test_log.py @@ -144,6 +144,9 @@ def get(self, section, option): def has_section(self, section): return False + def has_option(self, section, option): + return False + def test_weird_config1(): ConfiguredFormatter(MockConfig1())