Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move logging config to separate yaml, configure individual loggers #502

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
Expand Down
47 changes: 0 additions & 47 deletions scopesim/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions scopesim/logconfig.yaml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion scopesim/rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
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)))
except FileNotFoundError:
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([
Expand Down
3 changes: 1 addition & 2 deletions scopesim/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 4 additions & 13 deletions scopesim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)."""
Expand All @@ -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()


Expand All @@ -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()