diff --git a/pyproject.toml b/pyproject.toml index 8707f714..cb4cfea9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ScopeSim" -version = "0.9.1a2" +version = "0.9.1a3" description = "Generalised telescope observation simulator" license = "GPL-3.0-or-later" authors = ["Kieran Leschinski "] diff --git a/scopesim/defaults.yaml b/scopesim/defaults.yaml index b06f7db6..40ef9bcb 100644 --- a/scopesim/defaults.yaml +++ b/scopesim/defaults.yaml @@ -54,53 +54,6 @@ properties : image_format: "png" preamble_file: None - logging : - # This sub-dict enables direct configuration of logging. - # The corresponding schema can be found at: - # https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema - - version: 1 - disable_existing_loggers: False # Not sure what's best here?? - - root: # To allow e.g. warnings -> logging - level: INFO - handlers: [console] # [console, file] or just [console] - - loggers: - astar: - level: WARNING - handlers: [console] # [console, file] or just [console] - propagate: False # Any logging from astar stops here. - # Or don't add handlers here, but let it propagate to root? - # This doesn't work because NestedMapping doesn't like "." in keys... - # astar.scopesim: - # level: DEBUG - # propagate: True # Goes through to astar logger. - - handlers: - console: - class: logging.StreamHandler - level: INFO - formatter: color - stream: ext://sys.stdout - file: - class : logging.handlers.RotatingFileHandler - level: DEBUG - formatter: verbose - filename: ".scopesim.log" - mode: "w" # w - overwrite, a - append - encoding: "utf-8" - delay: True - maxBytes: 32768 - backupCount: 3 - - formatters: - verbose: - format: '%(asctime)s - %(levelname)-8s - %(name)s - %(funcName)s - %(message)s' - color: - '()': astar_utils.loggers.ColoredFormatter - show_name: True - tests : # overridden in tests/__init__.py run_integration_tests : True diff --git a/scopesim/logconfig.yaml b/scopesim/logconfig.yaml new file mode 100644 index 00000000..43837c1a --- /dev/null +++ b/scopesim/logconfig.yaml @@ -0,0 +1,54 @@ +### LOGGING CONFIGURATION FOR SCOPESIM +# The corresponding schema can be found at: +# https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema + +version: 1 # required +disable_existing_loggers: False # Not sure what's best here?? + +root: # To allow e.g. warnings -> logging + level: INFO + handlers: [console] # [console, file] or just [console] + +loggers: + astar: + level: WARNING + handlers: [console] # [console, file] or just [console] + propagate: False # Any logging from astar stops here. + # Or don't add handlers here, but let it propagate to root? + astar.scopesim: + level: DEBUG # Generally allow debug logging from ScopeSim. + propagate: True # Goes through to astar logger by default. + # The following loggers produce lots of log spam on DEBUG level, so switch + # them to INFO by default. They can be re-enabled individually if needed. + astar.scopesim.optics.image_plane: + level: INFO + astar.scopesim.optics.image_plane_utils: + level: INFO + astar.scopesim.optics.surface: + level: INFO + astar.scopesim.commands.user_commands: + level: INFO + +handlers: + console: + class: logging.StreamHandler + level: INFO + formatter: color + stream: ext://sys.stdout + file: + class : logging.handlers.RotatingFileHandler + level: DEBUG + formatter: verbose + filename: ".scopesim.log" + mode: "w" # w - overwrite, a - append + encoding: "utf-8" + delay: True + maxBytes: 32768 + backupCount: 3 + +formatters: + verbose: + format: '%(asctime)s - %(levelname)-8s - %(name)s - %(funcName)s - %(message)s' + color: + '()': astar_utils.loggers.ColoredFormatter + show_name: True diff --git a/scopesim/rc.py b/scopesim/rc.py index e66492e4..cf486656 100644 --- a/scopesim/rc.py +++ b/scopesim/rc.py @@ -13,7 +13,6 @@ with (__pkg_dir__ / "defaults.yaml").open(encoding="utf-8") as file: dicts = list(yaml.full_load_all(file)) - try: with (Path.home() / ".scopesim_rc.yaml").open(encoding="utf-8") as file: dicts.extend(list(yaml.full_load_all(file))) @@ -21,8 +20,13 @@ pass +with (__pkg_dir__ / "logconfig.yaml").open(encoding="utf-8") as file: + logconfig = yaml.full_load(file) + + __config__ = NestedMapping(dicts, title="SystemDict") __currsys__ = deepcopy(__config__) +__logging_config__ = logconfig # Order matters! __search_path__ = UniqueList([ diff --git a/scopesim/tests/test_logging.py b/scopesim/tests/test_logging.py index 11da49dd..4ab7ce66 100644 --- a/scopesim/tests/test_logging.py +++ b/scopesim/tests/test_logging.py @@ -25,8 +25,7 @@ def reload_scopesim(): @pytest.mark.usefixtures("reload_scopesim") def test_loggers_are_configured(): - log_dict = sim.rc.__config__["!SIM.logging"] - base_logger_dict = log_dict["loggers"]["astar"] + base_logger_dict = sim.rc.__logging_config__["loggers"]["astar"] base_logger = logging.getLogger("astar") sim_logger = base_logger.getChild("scopesim") diff --git a/scopesim/utils.py b/scopesim/utils.py index 0e42caed..345b19fa 100644 --- a/scopesim/utils.py +++ b/scopesim/utils.py @@ -700,20 +700,11 @@ def wrapper(*args, **kwargs): def update_logging(capture_warnings=True): - """Reload logging configuration from ``rc.__config__``.""" + """Reload logging configuration from ``rc.__logging_config__``.""" # Need to access NestedMapping's internal dict here... - # HACK: remove this try-except after updating the minimum astar-utils version... - try: - dictConfig(rc.__config__["!SIM.logging"].dic) - except AttributeError: - dictConfig(rc.__config__["!SIM.logging"]) + dictConfig(rc.__logging_config__) logging.captureWarnings(capture_warnings) - # This cannot be in the dict config (yet) because NestedMapping doesn't like - # "." in keys (yet) ... - # Set the "astar.scopesim" logger - get_logger(__package__).setLevel(logging.DEBUG) - def log_to_file(enable=True): """Enable or disable logging to file (convenience function).""" @@ -722,7 +713,7 @@ def log_to_file(enable=True): else: handlers = ["console"] - rc.__config__["!SIM.logging.loggers.astar.handlers"] = handlers + rc.__logging_config__["loggers"]["astar"]["handlers"] = handlers update_logging() @@ -732,5 +723,5 @@ def set_console_log_level(level="INFO"): This controls what is actually printed to the console by ScopeSim. Accepted values are: DEBUG, INFO (default), WARNING, ERROR and CRITICAL. """ - rc.__config__["!SIM.logging.handlers.console.level"] = level + rc.__logging_config__["handlers"]["console"]["level"] = level update_logging()