Skip to content

Commit

Permalink
Merge pull request #277 from SpiNNakerManchester/log_info_by_default
Browse files Browse the repository at this point in the history
Add logging to the config to make it happen
  • Loading branch information
Christian-B authored Aug 13, 2024
2 parents a525a82 + 5110cc1 commit 0e46a4f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
25 changes: 1 addition & 24 deletions spinn_utilities/conf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +24,7 @@
from spinn_utilities.configs import (
CamelCaseConfigParser, ConfigTemplateException,
NoConfigFoundException, UnexpectedConfigException)

logger = log.FormatAdapter(logging.getLogger(__name__))
_SectionParser: TypeAlias = Callable[[CamelCaseConfigParser], None]

Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
27 changes: 26 additions & 1 deletion spinn_utilities/config_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
Expand Down Expand Up @@ -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.
Expand All @@ -100,6 +123,8 @@ def load_config() -> CamelCaseConfigParser:
__config = CamelCaseConfigParser()
for default in __default_config_files:
__config.read(default)

logging_parser(__config)
return __config


Expand Down
7 changes: 5 additions & 2 deletions spinn_utilities/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions spinn_utilities/spinn_utilities.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion unittests/test_cfg_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])
3 changes: 3 additions & 0 deletions unittests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 0e46a4f

Please sign in to comment.