From 1aa53efd997d83f35847fb0bd5212db71b48b36a Mon Sep 17 00:00:00 2001 From: Rick Nitsche Date: Thu, 17 Sep 2020 17:23:48 -0700 Subject: [PATCH] feat(config): add logging config Use logging config from caput. --- bondia/data.py | 1 - bondia/gui.py | 1 - bondia/server.py | 11 +++++++++-- scripts/bondia-server | 15 ++++++++++----- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/bondia/data.py b/bondia/data.py index 46faeca..37a1572 100644 --- a/bondia/data.py +++ b/bondia/data.py @@ -19,7 +19,6 @@ from .util.exception import DataError logger = logging.getLogger(__name__) -logging.basicConfig(level=logging.INFO) FILE_TYPES = { "delayspectrum": "delayspectrum_lsd_*.h5", diff --git a/bondia/gui.py b/bondia/gui.py index 4367c5b..6a87639 100644 --- a/bondia/gui.py +++ b/bondia/gui.py @@ -7,7 +7,6 @@ logger = logging.getLogger(__name__) -logger.setLevel("DEBUG") class BondiaGui: diff --git a/bondia/server.py b/bondia/server.py index 589df35..e89f86c 100644 --- a/bondia/server.py +++ b/bondia/server.py @@ -1,4 +1,4 @@ -from caput.config import Property, Reader +from caput.config import Property, Reader, logging_config import holoviews as hv from jinja2 import Environment, PackageLoader from jinja2.exceptions import TemplateNotFound @@ -10,10 +10,10 @@ from .gui import BondiaGui logger = logging.getLogger(__name__) -logger.setLevel("DEBUG") class BondiaServer(Reader): + logging = logging_config(default={"root": "INFO"}) _config_data = Property({}, proptype=dict, key="data") _template_name = Property("mwc", str, "html_template") _width_drawer_widgets = Property(220, int) @@ -37,6 +37,13 @@ def __init__(self): raise ConfigError(f"Can't find template '{self._template_name}'.") def _finalise_config(self): + # Apply logging config + logging.basicConfig(level=getattr(logging, self.logging.get("root", "WARNING"))) + for module, level in self.logging.items(): + if module != "root": + logging.getLogger(module).setLevel(getattr(logging, level)) + logger.debug(f"Applied logging config: {self.logging}") + self.data = DataLoader.from_config(self._config_data) if not self.data.index: raise ConfigError("No data available.") diff --git a/scripts/bondia-server b/scripts/bondia-server index 815a7ac..62612b2 100755 --- a/scripts/bondia-server +++ b/scripts/bondia-server @@ -44,16 +44,21 @@ logger = logging.getLogger("bondia-server") default=True, show_default=True, ) -def start(configfile, show, port, num_procs, websocket_origin, login): +@click.option( + "--print-config/--no-print-config", + help="Print the config on start.", + default=True, + show_default=True, +) +def start(configfile, show, port, num_procs, websocket_origin, login, print_config): - # TODO: use caput.config - logging.basicConfig(level=logging.DEBUG) - logger.setLevel("DEBUG") + logging.basicConfig(level=logging.INFO) logger.info(f"Starting bondia-server {__version__} with config from {configfile}.") with pathlib.Path(configfile).open() as file: config = yaml.safe_load(file) - logger.debug(f"Loaded config: {config}") + if print_config: + logger.info(f"Loaded config: {config}") if isinstance(websocket_origin, str): websocket_origin = [websocket_origin]