From e9ac5cd947c8bcda1db71de072e3b65c069e8b6b Mon Sep 17 00:00:00 2001 From: gtrevisan Date: Mon, 14 Apr 2025 17:08:11 -0400 Subject: [PATCH] simplify log_settings --- disruption_py/settings/log_settings.py | 70 +++++++++++--------------- disruption_py/workflow.py | 2 +- examples/defaults.py | 9 ++-- tests/utils/eval_against_sql.py | 8 ++- 4 files changed, 37 insertions(+), 52 deletions(-) diff --git a/disruption_py/settings/log_settings.py b/disruption_py/settings/log_settings.py index 5ee1e9460..828dbcda6 100644 --- a/disruption_py/settings/log_settings.py +++ b/disruption_py/settings/log_settings.py @@ -31,27 +31,20 @@ class LogSettings: Attributes ---------- - log_file_path : str, optional + file_path : str, optional Path to the log file. If None, no log file will be created. By default, a log file will be created in a temporary folder. - file_log_level : str + file_level : str Logging level for the log file (default is "DEBUG"). Possible values are: "TRACE", "DEBUG", "VERBOSE" (custom), "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL". See: https://loguru.readthedocs.io/en/stable/api/logger.html#levels - log_file_write_mode : str, optional - The write mode for the log file. Default is "w". - log_to_console : bool - Whether to log messages to the console (default is True). - console_log_level : str or int, optional + console_level : str or int, optional The log level for the console. Default is None, so log level will be determined dynamically based on the number of shots. Possible values are: "TRACE", "DEBUG", "VERBOSE" (custom), "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL". See: https://loguru.readthedocs.io/en/stable/api/logger.html#levels - use_custom_logging : bool - Whether to use custom logging. If set to true, no logging setup will be done. - Default is False. warning_threshold : int If number of shots is greater than this threshold, the console log level will be "WARNING". Default is 1000. @@ -65,14 +58,9 @@ class LogSettings: Internal flag to prevent multiple setups (default is False). """ - log_file_path: str = os.path.join(get_temporary_folder(), "output.log") - file_log_level: str = "DEBUG" - log_file_write_mode: str = "w" - - log_to_console: bool = True - console_log_level: str = None - - use_custom_logging: bool = False + file_path: str = os.path.join(get_temporary_folder(), "output.log") + file_level: str = "DEBUG" + console_level: str = None warning_threshold: int = 1000 success_threshold: int = 500 @@ -97,7 +85,7 @@ def reset_handlers(self, num_shots: int = None): console_format = "{time:HH:mm:ss.SSS} " + message_format file_format = "{time:YYYY-MM-DD HH:mm:ss.SSS} " + message_format - if self.console_log_level is None: + if self.console_level is None: # Determine console log level dynamically based on the number of shots console_level = "VERBOSE" if num_shots and num_shots > self.warning_threshold: @@ -106,30 +94,29 @@ def reset_handlers(self, num_shots: int = None): console_level = "SUCCESS" elif num_shots and num_shots > self.info_threshold: console_level = "INFO" - elif isinstance(self.console_log_level, str): - console_level = self.console_log_level.upper() + elif isinstance(self.console_level, str): + console_level = self.console_level.upper() else: - console_level = self.console_log_level + console_level = self.console_level # Add console handler - if self.log_to_console: - logger.add( - lambda msg: tqdm.write(msg, end=""), - level=console_level, - format=console_format, - colorize=True, - enqueue=True, - backtrace=False, - diagnose=True, - ) + logger.add( + lambda msg: tqdm.write(msg, end=""), + level=console_level, + format=console_format, + colorize=True, + enqueue=True, + backtrace=False, + diagnose=True, + ) # Add file handler if log file path is provided - if self.log_file_path is not None: + if self.file_path is not None: logger.add( - self.log_file_path, - level=self.file_log_level, + self.file_path, + level=self.file_level, format=file_format, - mode=self.log_file_write_mode, + mode="w", enqueue=True, backtrace=False, diagnose=True, @@ -139,7 +126,7 @@ def setup_logging(self): """ Set up logging with custom styles and levels. """ - if self.use_custom_logging or self._logging_has_been_setup: + if self._logging_has_been_setup: return # Set custom style and add a VERBOSE level. This only needs to be done @@ -174,8 +161,8 @@ def setup_logging(self): u=os.getenv("USER"), h=os.uname().nodename, ) - if self.log_file_path is not None: - logger.info("Logging: {l}", l=self.log_file_path) + if self.file_path is not None: + logger.info("Logging: {l}", l=self.file_path) logger.debug( "Repository: {url}{append}{commit}", url="https://github.com/MIT-PSFC/disruption-py", @@ -208,7 +195,10 @@ def resolve_log_settings( return log_settings if isinstance(log_settings, (str, int)): - return LogSettings(console_log_level=log_settings) + return LogSettings(console_level=log_settings) + + if isinstance(log_settings, dict): + return LogSettings(**log_settings) if log_settings is None: return LogSettings() diff --git a/disruption_py/workflow.py b/disruption_py/workflow.py index ff1e7e1c4..f20d3b0b9 100644 --- a/disruption_py/workflow.py +++ b/disruption_py/workflow.py @@ -126,7 +126,7 @@ def get_shots_data( return None # Dynamically set the console log level based on the number of shots - if log_settings.console_log_level is None: + if log_settings.console_level is None: log_settings.reset_handlers(num_shots=len(shotlist_list)) # log start diff --git a/examples/defaults.py b/examples/defaults.py index a50940a04..a42894fd1 100644 --- a/examples/defaults.py +++ b/examples/defaults.py @@ -33,12 +33,9 @@ num_processes=1, log_settings=LogSettings( # default None: "output.log" in temporary session folder - log_file_path=None, - file_log_level="DEBUG", - log_file_write_mode="w", - log_to_console=True, + file_path=None, + file_level="DEBUG", # default None: VERBOSE, or higher based on number of shots - console_log_level=None, - use_custom_logging=False, + console_level=None, ), ) diff --git a/tests/utils/eval_against_sql.py b/tests/utils/eval_against_sql.py index 3e3d925e2..229d9dacc 100644 --- a/tests/utils/eval_against_sql.py +++ b/tests/utils/eval_against_sql.py @@ -61,11 +61,9 @@ def get_fresh_data( retrieval_settings=retrieval_settings, output_setting="dict", log_settings=LogSettings( - log_to_console=True, - log_file_path=log_file_path, - log_file_write_mode="w", - file_log_level="DEBUG", - console_log_level=console_log_level, + file_path=log_file_path, + file_level="DEBUG", + console_level=console_log_level, ), )