From 184a614d323928a2078c409193c87187e40fb50c Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 29 Aug 2024 13:58:40 -0600 Subject: [PATCH 01/12] initial logging for print statements that we added --- .gitignore | 1 + cupid/clear.py | 6 +++++- cupid/timeseries.py | 34 ++++++++++++++++++---------------- cupid/util.py | 5 ++++- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index eb2e3f2..7cca880 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ **/computed_notebooks/** **/cache_data_path/** **/cache_metadata_path/** +**/cupid.log **/temp_data/** **/ncfiles/** **/test_product.txt diff --git a/cupid/clear.py b/cupid/clear.py index d72d1b7..fa9d34d 100755 --- a/cupid/clear.py +++ b/cupid/clear.py @@ -10,6 +10,7 @@ """ from __future__ import annotations +import logging import os import shutil @@ -17,6 +18,9 @@ import cupid.util +logging.basicConfig(filename='cupid.log', level=logging.DEBUG) #, force=True) +logger = logging.getLogger(__name__) + def read_config_file(config_path): """ @@ -56,4 +60,4 @@ def clear(config_path): run_dir = read_config_file(config_path) # Delete the "computed_notebooks" folder and all the contents inside of it shutil.rmtree(run_dir) - print(f"All contents in {run_dir} have been cleared.") + logger.info(f"All contents in {run_dir} have been cleared.") diff --git a/cupid/timeseries.py b/cupid/timeseries.py index c1748ef..a2785a7 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -7,6 +7,7 @@ from __future__ import annotations import glob +import logging import multiprocessing as mp import os import subprocess @@ -14,6 +15,7 @@ import xarray as xr +logger = logging.getLogger(__name__) def call_ncrcat(cmd): """This is an internal function to `create_time_series` @@ -80,7 +82,7 @@ def create_time_series( """ # Notify user that script has started: - print(f"\n Generating {component} time series files...") + logger.info(f"\n Generating {component} time series files...") # Loop over cases: for case_idx, case_name in enumerate(case_names): @@ -90,11 +92,11 @@ def create_time_series( "Configuration file indicates time series files have been pre-computed" ) emsg += f" for case '{case_name}'. Will rely on those files directly." - print(emsg) + logger.info(emsg) continue # End if - print(f"\t Processing time series for case '{case_name}' :") + logger.info(f"\t Processing time series for case '{case_name}' :") # Extract start and end year values: start_year = start_years[case_idx] @@ -179,7 +181,7 @@ def create_time_series( wmsg += ( f" transferred beyond the {height_dim} dimension itself." ) - print(wmsg) + logger.warning(wmsg) vert_coord_type = None # End if @@ -190,7 +192,7 @@ def create_time_series( ) wmsg += " so no additional vertical coordinate information will be" wmsg += f" transferred beyond the {height_dim} dimension itself." - print(wmsg) + logger.warning(wmsg) vert_coord_type = None # End if (long name) @@ -223,13 +225,13 @@ def create_time_series( vars_to_derive = [] # create copy of var list that can be modified for derivable variables if diag_var_list == ["process_all"]: - print("generating time series for all variables") + logger.info("generating time series for all variables") # TODO: this does not seem to be working for ocn... diag_var_list = hist_file_var_list for var in diag_var_list: if var not in hist_file_var_list: if component == "ocn": - print( + logger.warning( "ocean vars seem to not be present in all files and thus cause errors", ) continue @@ -244,7 +246,7 @@ def create_time_series( continue msg = f"WARNING: {var} is not in the file {hist_files[0]}." msg += " No time series will be generated." - print(msg) + logger.warning(msg) continue # Check if variable has a height_dim (eg, 'lev') dimension according to first file: @@ -269,7 +271,7 @@ def create_time_series( continue # Notify user of new time series file: - print(f"\t - time series for {var}") + logger.info(f"\t - time series for {var}") # Variable list starts with just the variable ncrcat_var_list = f"{var}" @@ -288,11 +290,11 @@ def create_time_series( if "PS" in hist_file_var_list: ncrcat_var_list = ncrcat_var_list + ",PS" - print("Adding PS to file") + logger.info("Adding PS to file") else: wmsg = "WARNING: PS not found in history file." wmsg += " It might be needed at some point." - print(wmsg) + logger.warning(wmsg) # End if if vert_coord_type == "height": @@ -304,11 +306,11 @@ def create_time_series( # PMID file to each one of those targets separately. -JN if "PMID" in hist_file_var_list: ncrcat_var_list = ncrcat_var_list + ",PMID" - print("Adding PMID to file") + logger.info("Adding PMID to file") else: wmsg = "WARNING: PMID not found in history file." wmsg += " It might be needed at some point." - print(wmsg) + logger.warning(wmsg) # End if PMID # End if height # End if cam @@ -342,7 +344,7 @@ def create_time_series( # End cases loop # Notify user that script has ended: - print(f" ... {component} time series file generation has finished successfully.") + logger.info(f" ... {component} time series file generation has finished successfully.") def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None): @@ -376,7 +378,7 @@ def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None): if overwrite: Path(prect_file).unlink() else: - print( + logger.warning( f"[{__name__}] Warning: PRECT file was found and overwrite is False" + "Will use existing file.", ) @@ -410,7 +412,7 @@ def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None): if overwrite: Path(derived_file).unlink() else: - print( + logger.warning( f"[{__name__}] Warning: RESTOM file was found and overwrite is False." + "Will use existing file.", ) diff --git a/cupid/util.py b/cupid/util.py index 2d5b9cc..f4f6c2e 100644 --- a/cupid/util.py +++ b/cupid/util.py @@ -15,6 +15,7 @@ """ from __future__ import annotations +import logging import os import sys import warnings @@ -27,6 +28,8 @@ from jinja2 import Template from papermill.engines import NBClientEngine +logger = logging.getLogger(__name__) + class MarkdownJinjaEngine(NBClientEngine): """Class for using the Jinja Engine to run notebooks""" @@ -50,7 +53,7 @@ def get_control_dict(config_path): with open(config_path) as fid: control = yaml.safe_load(fid) except FileNotFoundError: - print(f"ERROR: {config_path} not found") + logger.error(f"ERROR: {config_path} not found") sys.exit(1) default_kernel_name = control["computation_config"].pop("default_kernel_name", None) From b83d8a1d20744955a2f4a2eb9a6dd0a8e17a8e2a Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 29 Aug 2024 14:01:05 -0600 Subject: [PATCH 02/12] pre commits --- cupid/clear.py | 2 +- cupid/timeseries.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cupid/clear.py b/cupid/clear.py index fa9d34d..a0edc6c 100755 --- a/cupid/clear.py +++ b/cupid/clear.py @@ -18,7 +18,7 @@ import cupid.util -logging.basicConfig(filename='cupid.log', level=logging.DEBUG) #, force=True) +logging.basicConfig(filename="cupid.log", level=logging.DEBUG) # , force=True) logger = logging.getLogger(__name__) diff --git a/cupid/timeseries.py b/cupid/timeseries.py index a2785a7..0d3b167 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -17,6 +17,7 @@ logger = logging.getLogger(__name__) + def call_ncrcat(cmd): """This is an internal function to `create_time_series` It just wraps the subprocess.call() function, so it can be @@ -344,7 +345,9 @@ def create_time_series( # End cases loop # Notify user that script has ended: - logger.info(f" ... {component} time series file generation has finished successfully.") + logger.info( + f" ... {component} time series file generation has finished successfully.", + ) def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None): From c42f4fcf383e41c0e2f104320ad9ceb8af4fc14f Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 29 Aug 2024 14:34:27 -0600 Subject: [PATCH 03/12] formatting --- .gitignore | 2 +- cupid/clear.py | 6 +++++- cupid/timeseries.py | 5 +++++ cupid/util.py | 5 +++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7cca880..383a459 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ **/computed_notebooks/** **/cache_data_path/** **/cache_metadata_path/** -**/cupid.log +**/cupid.log* **/temp_data/** **/ncfiles/** **/test_product.txt diff --git a/cupid/clear.py b/cupid/clear.py index a0edc6c..8c78b4a 100755 --- a/cupid/clear.py +++ b/cupid/clear.py @@ -13,12 +13,16 @@ import logging import os import shutil +from datetime import datetime import click import cupid.util -logging.basicConfig(filename="cupid.log", level=logging.DEBUG) # , force=True) +logging.basicConfig( + filename="cupid.log.{}".format(datetime.now().strftime("%Y%m%d%H%M%S")), + level=logging.DEBUG, +) logger = logging.getLogger(__name__) diff --git a/cupid/timeseries.py b/cupid/timeseries.py index 0d3b167..1a610d8 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -11,10 +11,15 @@ import multiprocessing as mp import os import subprocess +from datetime import datetime from pathlib import Path import xarray as xr +logging.basicConfig( + filename="cupid.log.{}".format(datetime.now().strftime("%Y%m%d%H%M%S")), + level=logging.DEBUG, +) logger = logging.getLogger(__name__) diff --git a/cupid/util.py b/cupid/util.py index f4f6c2e..e592357 100644 --- a/cupid/util.py +++ b/cupid/util.py @@ -19,6 +19,7 @@ import os import sys import warnings +from datetime import datetime from pathlib import Path import jupyter_client @@ -28,6 +29,10 @@ from jinja2 import Template from papermill.engines import NBClientEngine +logging.basicConfig( + filename="cupid.log.{}".format(datetime.now().strftime("%Y%m%d%H%M%S")), + level=logging.DEBUG, +) logger = logging.getLogger(__name__) From f8db211b8f3f102aaf425746e0ffb239eca417e9 Mon Sep 17 00:00:00 2001 From: Teagan King Date: Wed, 4 Sep 2024 17:26:25 -0600 Subject: [PATCH 04/12] remove filename --- .gitignore | 1 - cupid/clear.py | 2 -- cupid/timeseries.py | 2 -- cupid/util.py | 2 -- 4 files changed, 7 deletions(-) diff --git a/.gitignore b/.gitignore index 383a459..eb2e3f2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ **/computed_notebooks/** **/cache_data_path/** **/cache_metadata_path/** -**/cupid.log* **/temp_data/** **/ncfiles/** **/test_product.txt diff --git a/cupid/clear.py b/cupid/clear.py index 8c78b4a..a29d25b 100755 --- a/cupid/clear.py +++ b/cupid/clear.py @@ -13,14 +13,12 @@ import logging import os import shutil -from datetime import datetime import click import cupid.util logging.basicConfig( - filename="cupid.log.{}".format(datetime.now().strftime("%Y%m%d%H%M%S")), level=logging.DEBUG, ) logger = logging.getLogger(__name__) diff --git a/cupid/timeseries.py b/cupid/timeseries.py index 1a610d8..f040388 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -11,13 +11,11 @@ import multiprocessing as mp import os import subprocess -from datetime import datetime from pathlib import Path import xarray as xr logging.basicConfig( - filename="cupid.log.{}".format(datetime.now().strftime("%Y%m%d%H%M%S")), level=logging.DEBUG, ) logger = logging.getLogger(__name__) diff --git a/cupid/util.py b/cupid/util.py index e592357..fc5a59e 100644 --- a/cupid/util.py +++ b/cupid/util.py @@ -19,7 +19,6 @@ import os import sys import warnings -from datetime import datetime from pathlib import Path import jupyter_client @@ -30,7 +29,6 @@ from papermill.engines import NBClientEngine logging.basicConfig( - filename="cupid.log.{}".format(datetime.now().strftime("%Y%m%d%H%M%S")), level=logging.DEBUG, ) logger = logging.getLogger(__name__) From f1c599b19c475760c619d53fa5919ab92c9a9298 Mon Sep 17 00:00:00 2001 From: Teagan Date: Tue, 17 Sep 2024 15:26:59 -0600 Subject: [PATCH 05/12] include log level in config file and move to utils --- cupid/clear.py | 8 +----- cupid/timeseries.py | 10 ++++---- cupid/util.py | 41 ++++++++++++++++++++++++++----- examples/coupled_model/config.yml | 5 +++- examples/key_metrics/config.yml | 5 +++- 5 files changed, 49 insertions(+), 20 deletions(-) diff --git a/cupid/clear.py b/cupid/clear.py index a29d25b..8cb6b4f 100755 --- a/cupid/clear.py +++ b/cupid/clear.py @@ -10,7 +10,6 @@ """ from __future__ import annotations -import logging import os import shutil @@ -18,11 +17,6 @@ import cupid.util -logging.basicConfig( - level=logging.DEBUG, -) -logger = logging.getLogger(__name__) - def read_config_file(config_path): """ @@ -58,7 +52,7 @@ def clear(config_path): Args: CONFIG_PATH - The path to the configuration file. """ - + logger = cupid.util.setup_logging(config_path) run_dir = read_config_file(config_path) # Delete the "computed_notebooks" folder and all the contents inside of it shutil.rmtree(run_dir) diff --git a/cupid/timeseries.py b/cupid/timeseries.py index f040388..091d72f 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -7,7 +7,6 @@ from __future__ import annotations import glob -import logging import multiprocessing as mp import os import subprocess @@ -15,10 +14,11 @@ import xarray as xr -logging.basicConfig( - level=logging.DEBUG, -) -logger = logging.getLogger(__name__) +import cupid.util + + +# TODO: bring in actual config path in case not using config.yml +logger = cupid.util.setup_logging("config.yml") def call_ncrcat(cmd): diff --git a/cupid/util.py b/cupid/util.py index fc5a59e..cdf3ca2 100644 --- a/cupid/util.py +++ b/cupid/util.py @@ -28,11 +28,6 @@ from jinja2 import Template from papermill.engines import NBClientEngine -logging.basicConfig( - level=logging.DEBUG, -) -logger = logging.getLogger(__name__) - class MarkdownJinjaEngine(NBClientEngine): """Class for using the Jinja Engine to run notebooks""" @@ -52,11 +47,12 @@ def execute_managed_notebook(cls, nb_man, kernel_name, **kwargs): def get_control_dict(config_path): """Get control dictionary from configuration file""" + try: with open(config_path) as fid: control = yaml.safe_load(fid) except FileNotFoundError: - logger.error(f"ERROR: {config_path} not found") + print(f"ERROR: {config_path} not found") sys.exit(1) default_kernel_name = control["computation_config"].pop("default_kernel_name", None) @@ -99,6 +95,39 @@ def get_control_dict(config_path): return control +def setup_logging(config_path): + """ + Set up logging based on configuration file log level + Returns logger object + """ + control = get_control_dict(config_path) + log_level = control["computation_config"].get("log_level", None) + if log_level: + if log_level == "debug": + logging.basicConfig( + level=logging.DEBUG, + ) + if log_level == "info": + logging.basicConfig( + level=logging.INFO, + ) + if log_level == "warning": + logging.basicConfig( + level=logging.WARNING, + ) + if log_level == "error": + logging.basicConfig( + level=logging.ERROR, + ) + else: + # default level is warning if log level is not set in config + logging.basicConfig( + level=logging.WARNING, + ) + + return logging.getLogger(__name__) + + def setup_book(config_path): """Setup run directory and output jupyter book""" diff --git a/examples/coupled_model/config.yml b/examples/coupled_model/config.yml index 4fdabc0..f9049b3 100644 --- a/examples/coupled_model/config.yml +++ b/examples/coupled_model/config.yml @@ -30,9 +30,12 @@ computation_config: ### It must already be installed on your machine. You can also ### specify a different environment than the default for any ### notebook in NOTEBOOK CONFIG - default_kernel_name: cupid-analysis + # log level sets the level of how verbose logging will be. + # options include: debug, info, warning, error + log_level: info + ############# NOTEBOOK CONFIG ############# ############################ diff --git a/examples/key_metrics/config.yml b/examples/key_metrics/config.yml index 0bd44e2..664b994 100644 --- a/examples/key_metrics/config.yml +++ b/examples/key_metrics/config.yml @@ -30,9 +30,12 @@ computation_config: ### It must already be installed on your machine. You can also ### specify a different environment than the default for any ### notebook in NOTEBOOK CONFIG - default_kernel_name: cupid-analysis + # log level sets the level of how verbose logging will be. + # options include: debug, info, warning, error + log_level: info + ############# NOTEBOOK CONFIG ############# ############################ From 84a41eb20bf13bbb860c8e61a6aea755cbe1b936 Mon Sep 17 00:00:00 2001 From: Teagan Date: Wed, 18 Sep 2024 09:41:09 -0600 Subject: [PATCH 06/12] pass logger argument in timeseries and update warnings.warn to logger.warning --- cupid/run.py | 11 ++++++----- cupid/timeseries.py | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cupid/run.py b/cupid/run.py index 0686bda..9515e1f 100755 --- a/cupid/run.py +++ b/cupid/run.py @@ -23,7 +23,6 @@ from __future__ import annotations import os -import warnings import click import intake @@ -74,6 +73,7 @@ def run( # Get control structure control = cupid.util.get_control_dict(config_path) cupid.util.setup_book(config_path) + logger = cupid.util.setup_logging(config_path) component_options = { "atm": atmosphere, @@ -145,6 +145,7 @@ def run( timeseries_params[component]["level"], num_procs, serial, + logger, ) # fmt: on # pylint: enable=line-too-long @@ -210,7 +211,7 @@ def run( all_nbs[nb]["nb_path_root"] = nb_path_root + "/" + comp_name all_nbs[nb]["output_dir"] = output_dir + "/" + comp_name elif comp_bool and not all: - warnings.warn( + logger.warning( f"No notebooks for {comp_name} component specified in config file.", ) @@ -219,7 +220,7 @@ def run( for nb, info in all_nbs.copy().items(): if not control["env_check"][info["kernel_name"]]: bad_env = info["kernel_name"] - warnings.warn( + logger.warning( f"Environment {bad_env} specified for {nb}.ipynb could not be found;" + f" {nb}.ipynb will not be run." + "See README.md for environment installation instructions.", @@ -253,7 +254,7 @@ def run( all_scripts[script] = info all_scripts[script]["nb_path_root"] = nb_path_root + "/" + comp_name elif comp_bool and not all: - warnings.warn( + logger.warning( f"No scripts for {comp_name} component specified in config file.", ) @@ -262,7 +263,7 @@ def run( for script, info in all_scripts.copy().items(): if not control["env_check"][info["kernel_name"]]: bad_env = info["kernel_name"] - warnings.warn( + logger.warning( f"Environment {bad_env} specified for {script}.py could not be found;" + f"{script}.py will not be run.", ) diff --git a/cupid/timeseries.py b/cupid/timeseries.py index 091d72f..d138454 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -17,10 +17,6 @@ import cupid.util -# TODO: bring in actual config path in case not using config.yml -logger = cupid.util.setup_logging("config.yml") - - def call_ncrcat(cmd): """This is an internal function to `create_time_series` It just wraps the subprocess.call() function, so it can be @@ -45,6 +41,7 @@ def create_time_series( height_dim, num_procs, serial, + logger, ): """ Generate time series versions of the history file data. @@ -336,6 +333,7 @@ def create_time_series( derive_cam_variables( vars_to_derive=vars_to_derive, ts_dir=ts_dir[case_idx], + logger=logger, ) if serial: @@ -353,7 +351,7 @@ def create_time_series( ) -def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None): +def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None, logger=None): """ Derive variables acccording to steps given here. Since derivations will depend on the variable, each variable to derive will need its own set of steps below. @@ -364,6 +362,9 @@ def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None): whether to overwrite the file (true) or exit with a warning message. """ + if logger==None: + logger = cupid.util.setup_logging("config.yml") + for var in vars_to_derive: if var == "PRECT": # PRECT can be found by simply adding PRECL and PRECC From 602af5f9dc4aafc97c377c6c9f48e40016639e25 Mon Sep 17 00:00:00 2001 From: Teagan Date: Wed, 18 Sep 2024 09:43:06 -0600 Subject: [PATCH 07/12] precommit updates --- cupid/timeseries.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cupid/timeseries.py b/cupid/timeseries.py index d138454..6c9c91f 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -362,8 +362,8 @@ def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None, logge whether to overwrite the file (true) or exit with a warning message. """ - if logger==None: - logger = cupid.util.setup_logging("config.yml") + if logger is None: + logger = cupid.util.setup_logging("config.yml") for var in vars_to_derive: if var == "PRECT": From 9e6574d0525cd9f8cefe8b33ed435fd07fc575c7 Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 19 Sep 2024 09:16:37 -0600 Subject: [PATCH 08/12] change default logging level to info since that's probably better while in development --- cupid/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cupid/util.py b/cupid/util.py index cdf3ca2..5af6553 100644 --- a/cupid/util.py +++ b/cupid/util.py @@ -120,9 +120,9 @@ def setup_logging(config_path): level=logging.ERROR, ) else: - # default level is warning if log level is not set in config + # default level is info if log level is not set in config logging.basicConfig( - level=logging.WARNING, + level=logging.INFO, ) return logging.getLogger(__name__) From 784086f369234f43ef613291b091249b931e3101 Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 19 Sep 2024 10:35:03 -0600 Subject: [PATCH 09/12] updates from review --- cupid/timeseries.py | 11 +++-------- cupid/util.py | 38 +++++++++++++++++--------------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/cupid/timeseries.py b/cupid/timeseries.py index a0dc7ba..4f61dba 100644 --- a/cupid/timeseries.py +++ b/cupid/timeseries.py @@ -14,8 +14,6 @@ import xarray as xr -import cupid.util - def call_ncrcat(cmd): """This is an internal function to `create_time_series` @@ -84,7 +82,7 @@ def create_time_series( # Don't do anything if list of requested diagnostics is empty if not diag_var_list: - print(f"\n No time series files requested for {component}...") + logger.info(f"\n No time series files requested for {component}...") return # Notify user that script has started: @@ -336,9 +334,9 @@ def create_time_series( if vars_to_derive: if component == "atm": derive_cam_variables( + logger, vars_to_derive=vars_to_derive, ts_dir=ts_dir[case_idx], - logger=logger, ) if serial: @@ -356,7 +354,7 @@ def create_time_series( ) -def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None, logger=None): +def derive_cam_variables(logger, vars_to_derive=None, ts_dir=None, overwrite=None): """ Derive variables acccording to steps given here. Since derivations will depend on the variable, each variable to derive will need its own set of steps below. @@ -367,9 +365,6 @@ def derive_cam_variables(vars_to_derive=None, ts_dir=None, overwrite=None, logge whether to overwrite the file (true) or exit with a warning message. """ - if logger is None: - logger = cupid.util.setup_logging("config.yml") - for var in vars_to_derive: if var == "PRECT": # PRECT can be found by simply adding PRECL and PRECC diff --git a/cupid/util.py b/cupid/util.py index 5af6553..adbe0f9 100644 --- a/cupid/util.py +++ b/cupid/util.py @@ -4,7 +4,8 @@ Functions: - get_control_dict(): Get the control dictionary from a configuration file. - - setup_book(): Setup run dir and output Jupyter book based on config.yaml + - setup_logging(): Set up logging based on configuration file log level. + - setup_book(): Setup run dir and output Jupyter book based on config.yaml. - get_toc_files(): Return a list of files in the '_toc.yml'. - create_ploomber_nb_task(): Create a Ploomber task for running a notebook. - create_ploomber_script_task(): Create a Ploomber task for running a script. @@ -101,29 +102,24 @@ def setup_logging(config_path): Returns logger object """ control = get_control_dict(config_path) - log_level = control["computation_config"].get("log_level", None) - if log_level: - if log_level == "debug": - logging.basicConfig( - level=logging.DEBUG, - ) - if log_level == "info": - logging.basicConfig( - level=logging.INFO, - ) - if log_level == "warning": - logging.basicConfig( - level=logging.WARNING, - ) - if log_level == "error": - logging.basicConfig( - level=logging.ERROR, - ) - else: - # default level is info if log level is not set in config + # default level is info if log level is not set in config + log_level = control["computation_config"].get("log_level", "info") + if log_level == "debug": + logging.basicConfig( + level=logging.DEBUG, + ) + if log_level == "info": logging.basicConfig( level=logging.INFO, ) + if log_level == "warning": + logging.basicConfig( + level=logging.WARNING, + ) + if log_level == "error": + logging.basicConfig( + level=logging.ERROR, + ) return logging.getLogger(__name__) From 72c8cd6349e53fcbf0d88594e0a2c44cc64167f4 Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 19 Sep 2024 14:47:21 -0600 Subject: [PATCH 10/12] include else statement in case of log level with typo --- cupid/util.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cupid/util.py b/cupid/util.py index adbe0f9..851303e 100644 --- a/cupid/util.py +++ b/cupid/util.py @@ -99,27 +99,33 @@ def get_control_dict(config_path): def setup_logging(config_path): """ Set up logging based on configuration file log level + Options for log levels include debug, info, warning, and error. Returns logger object """ control = get_control_dict(config_path) # default level is info if log level is not set in config log_level = control["computation_config"].get("log_level", "info") - if log_level == "debug": + if log_level == "debug" or log_level == "DEBUG": logging.basicConfig( level=logging.DEBUG, ) - if log_level == "info": + elif log_level == "info" or log_level == "INFO": logging.basicConfig( level=logging.INFO, ) - if log_level == "warning": + elif log_level == "warning" or log_level == "WARNING": logging.basicConfig( level=logging.WARNING, ) - if log_level == "error": + elif log_level == "error" or log_level == "ERROR": logging.basicConfig( level=logging.ERROR, ) + else: + print('setting log_level to "info" because invalid log level') + logging.basicConfig( + level=logging.INFO, + ) return logging.getLogger(__name__) From 889c0306bb82e5a9866f87b0ee5e6b9d1038cdb0 Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 19 Sep 2024 15:17:01 -0600 Subject: [PATCH 11/12] add quotes around log level in config! --- examples/key_metrics/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/key_metrics/config.yml b/examples/key_metrics/config.yml index 2fed401..e559ee7 100644 --- a/examples/key_metrics/config.yml +++ b/examples/key_metrics/config.yml @@ -34,7 +34,7 @@ computation_config: # log level sets the level of how verbose logging will be. # options include: debug, info, warning, error - log_level: info + log_level: 'info' ############# NOTEBOOK CONFIG ############# From 604c5bc790ac99c1a30a300edb20f576d4659b94 Mon Sep 17 00:00:00 2001 From: Teagan King Date: Thu, 19 Sep 2024 15:18:22 -0600 Subject: [PATCH 12/12] add quotes in coupled_model config --- examples/coupled_model/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/coupled_model/config.yml b/examples/coupled_model/config.yml index f9049b3..bf91a81 100644 --- a/examples/coupled_model/config.yml +++ b/examples/coupled_model/config.yml @@ -34,7 +34,7 @@ computation_config: # log level sets the level of how verbose logging will be. # options include: debug, info, warning, error - log_level: info + log_level: 'info' ############# NOTEBOOK CONFIG #############