diff --git a/sed/calibrator/delay.py b/sed/calibrator/delay.py index c4d8f797..d465a462 100644 --- a/sed/calibrator/delay.py +++ b/sed/calibrator/delay.py @@ -5,8 +5,6 @@ from collections.abc import Sequence from copy import deepcopy from datetime import datetime -from logging import INFO -from logging import WARNING from typing import Any import dask.dataframe @@ -15,6 +13,7 @@ import pandas as pd from sed.core import dfops +from sed.core.logging import set_verbosity from sed.core.logging import setup_logging # Configure logging @@ -50,10 +49,7 @@ def __init__( self._config = {} self.verbose = verbose - if self.verbose: - logger.handlers[0].setLevel(INFO) - else: - logger.handlers[0].setLevel(WARNING) + set_verbosity(logger, self.verbose) self.adc_column: str = self._config["dataframe"].get("adc_column", None) self.delay_column: str = self._config["dataframe"]["delay_column"] diff --git a/sed/calibrator/energy.py b/sed/calibrator/energy.py index ca7c2f10..1e50d745 100644 --- a/sed/calibrator/energy.py +++ b/sed/calibrator/energy.py @@ -8,8 +8,6 @@ from copy import deepcopy from datetime import datetime from functools import partial -from logging import INFO -from logging import WARNING from typing import Any from typing import cast from typing import Literal @@ -37,6 +35,7 @@ from sed.binning import bin_dataframe from sed.core import dfops +from sed.core.logging import set_verbosity from sed.core.logging import setup_logging from sed.loader.base.loader import BaseLoader @@ -93,10 +92,7 @@ def __init__( self._config = config self.verbose = verbose - if self.verbose: - logger.handlers[0].setLevel(INFO) - else: - logger.handlers[0].setLevel(WARNING) + set_verbosity(logger, self.verbose) self.loader = loader self.biases: np.ndarray = None diff --git a/sed/calibrator/momentum.py b/sed/calibrator/momentum.py index bae3bf65..ac26f0e7 100644 --- a/sed/calibrator/momentum.py +++ b/sed/calibrator/momentum.py @@ -6,8 +6,6 @@ import itertools as it from copy import deepcopy from datetime import datetime -from logging import INFO -from logging import WARNING from typing import Any import bokeh.palettes as bp @@ -34,6 +32,7 @@ from symmetrize import sym from symmetrize import tps +from sed.core.logging import set_verbosity from sed.core.logging import setup_logging # Configure logging @@ -81,10 +80,7 @@ def __init__( self._config = config self.verbose = verbose - if self.verbose: - logger.handlers[0].setLevel(INFO) - else: - logger.handlers[0].setLevel(WARNING) + set_verbosity(logger, self.verbose) self.image: np.ndarray = None self.img_ndim: int = None diff --git a/sed/core/logging.py b/sed/core/logging.py index 539f29b6..ad019dc8 100644 --- a/sed/core/logging.py +++ b/sed/core/logging.py @@ -29,7 +29,8 @@ def setup_logging( Args: name (str): The name of the logger. - base_name (str, optional): The name of the base logger. Defaults to "sed". + set_base_handler (bool, optional): Option to re-initialize the base handler logging to the + logfile. Defaults to False. user_log_path (str, optional): Path to the user-specific log directory. Defaults to DEFAULT_LOG_DIR. @@ -88,6 +89,20 @@ def setup_logging( return logger +def set_verbosity(logger: logging.Logger, verbose: bool) -> None: + """Sets log level for the given logger's default handler. + + Args: + logger (logging.Logger): The logger on which to set the log level. + verbose (bool): Sets loglevel to INFO if True, to WARNING otherwise. + """ + handler = logger.handlers[0] + if verbose: + handler.setLevel(logging.INFO) + else: + handler.setLevel(logging.WARNING) + + def call_logger(logger: logging.Logger): def log_call(func: Callable): @wraps(func) diff --git a/sed/core/processor.py b/sed/core/processor.py index 247e13f4..ea3ac37e 100644 --- a/sed/core/processor.py +++ b/sed/core/processor.py @@ -6,8 +6,6 @@ import pathlib from collections.abc import Sequence from datetime import datetime -from logging import INFO -from logging import WARNING from typing import Any from typing import cast @@ -30,6 +28,7 @@ from sed.core.dfops import apply_filter from sed.core.dfops import apply_jitter from sed.core.logging import call_logger +from sed.core.logging import set_verbosity from sed.core.logging import setup_logging from sed.core.metadata import MetaHandler from sed.diagnostics import grid_histogram @@ -121,10 +120,7 @@ def __init__( self.verbose = self._config["core"].get("verbose", True) else: self.verbose = verbose - if self.verbose: - logger.handlers[0].setLevel(INFO) - else: - logger.handlers[0].setLevel(WARNING) + set_verbosity(logger, self.verbose) self._dataframe: pd.DataFrame | ddf.DataFrame = None self._timed_dataframe: pd.DataFrame | ddf.DataFrame = None diff --git a/sed/loader/flash/loader.py b/sed/loader/flash/loader.py index b44b3b82..b17621fd 100644 --- a/sed/loader/flash/loader.py +++ b/sed/loader/flash/loader.py @@ -12,13 +12,12 @@ import re import time from collections.abc import Sequence -from logging import INFO -from logging import WARNING from pathlib import Path import dask.dataframe as dd from natsort import natsorted +from sed.core.logging import set_verbosity from sed.core.logging import setup_logging from sed.loader.base.loader import BaseLoader from sed.loader.flash.buffer_handler import BufferHandler @@ -55,10 +54,7 @@ def __init__(self, config: dict, verbose: bool = True) -> None: """ super().__init__(config=config, verbose=verbose) - if self.verbose: - logger.handlers[0].setLevel(INFO) - else: - logger.handlers[0].setLevel(WARNING) + set_verbosity(logger, self.verbose) self.instrument: str = self._config["core"].get("instrument", "hextof") # default is hextof self.raw_dir: str = None diff --git a/sed/loader/mpes/loader.py b/sed/loader/mpes/loader.py index df4d0323..c130308a 100644 --- a/sed/loader/mpes/loader.py +++ b/sed/loader/mpes/loader.py @@ -10,8 +10,6 @@ import json import os from collections.abc import Sequence -from logging import INFO -from logging import WARNING from typing import Any from urllib.error import HTTPError from urllib.error import URLError @@ -25,6 +23,7 @@ import scipy.interpolate as sint from natsort import natsorted +from sed.core.logging import set_verbosity from sed.core.logging import setup_logging from sed.loader.base.loader import BaseLoader @@ -571,10 +570,7 @@ def __init__( ): super().__init__(config=config, verbose=verbose) - if self.verbose: - logger.handlers[0].setLevel(INFO) - else: - logger.handlers[0].setLevel(WARNING) + set_verbosity(logger, self.verbose) self.read_timestamps = self._config.get("dataframe", {}).get( "read_timestamps", diff --git a/sed/loader/sxp/loader.py b/sed/loader/sxp/loader.py index e18a64f9..1c6d625e 100644 --- a/sed/loader/sxp/loader.py +++ b/sed/loader/sxp/loader.py @@ -14,8 +14,6 @@ import time from collections.abc import Sequence from functools import reduce -from logging import INFO -from logging import WARNING from pathlib import Path import dask.dataframe as dd @@ -30,6 +28,7 @@ from pandas import Series from sed.core import dfops +from sed.core.logging import set_verbosity from sed.core.logging import setup_logging from sed.loader.base.loader import BaseLoader from sed.loader.utils import parse_h5_keys @@ -57,10 +56,7 @@ class SXPLoader(BaseLoader): def __init__(self, config: dict, verbose: bool = True) -> None: super().__init__(config=config, verbose=verbose) - if self.verbose: - logger.handlers[0].setLevel(INFO) - else: - logger.handlers[0].setLevel(WARNING) + set_verbosity(logger, self.verbose) self.multi_index = ["trainId", "pulseId", "electronId"] self.index_per_electron: MultiIndex = None