diff --git a/src/ansys/pytwin/evaluate/twin_model.py b/src/ansys/pytwin/evaluate/twin_model.py index 8b024c1a..331c8ed7 100644 --- a/src/ansys/pytwin/evaluate/twin_model.py +++ b/src/ansys/pytwin/evaluate/twin_model.py @@ -114,6 +114,8 @@ def _create_dataframe_inputs(self, inputs_df: pd.DataFrame): @staticmethod def _get_runtime_log_level(): + if not pytwin_logging_is_enabled(): + return LogLevel.TWIN_NO_LOG pytwin_level = get_pytwin_log_level() if pytwin_level == PyTwinLogLevel.PYTWIN_LOG_DEBUG: return LogLevel.TWIN_LOG_ALL @@ -125,8 +127,6 @@ def _get_runtime_log_level(): return LogLevel.TWIN_LOG_ERROR if pytwin_level == PyTwinLogLevel.PYTWIN_LOG_CRITICAL: return LogLevel.TWIN_LOG_FATAL - if not pytwin_logging_is_enabled(): - return LogLevel.TWIN_NO_LOG def _initialize_evaluation(self, parameters: dict = None, inputs: dict = None): """ @@ -224,7 +224,9 @@ def _instantiate_twin_model(self): self._model_name = self._twin_runtime.twin_get_model_name() if not os.path.exists(self.model_dir): os.mkdir(self.model_dir) - os.link(self.model_log, self.model_log_link) + # Create link to log file if any + if os.path.exists(self.model_log): + os.link(self.model_log, self.model_log_link) # Update TwinModel variables self._instantiation_time = time.time() diff --git a/src/ansys/pytwin/settings.py b/src/ansys/pytwin/settings.py index 520db8d6..53a87790 100644 --- a/src/ansys/pytwin/settings.py +++ b/src/ansys/pytwin/settings.py @@ -407,6 +407,8 @@ def modify_logging(new_option: PyTwinLogOption, new_level: PyTwinLogLevel): # Modifications in case of new level if new_level is not None: if new_level != _PyTwinSettings.LOGGING_LEVEL: + # fix ACE bug on logging level Jan 18th 2023 (test_modify_logging_level) + _PyTwinSettings.LOGGING_LEVEL = new_level pytwin_logger.setLevel(new_level.value) for handler in pytwin_logger.handlers: handler.setLevel(new_level.value) diff --git a/tests/evaluate/test_twin_model_logging.py b/tests/evaluate/test_twin_model_logging.py new file mode 100644 index 00000000..8a4a1f15 --- /dev/null +++ b/tests/evaluate/test_twin_model_logging.py @@ -0,0 +1,36 @@ +import os + +from pytwin import PYTWIN_LOGGING_OPT_NOLOGGING, TwinModel +from pytwin.settings import get_pytwin_log_file, modify_pytwin_logging + +COUPLE_CLUTCHES_FILEPATH = os.path.join(os.path.dirname(__file__), "data", "CoupleClutches_22R2_other.twin") +UNIT_TEST_WD = os.path.join(os.path.dirname(__file__), "unit_test_wd") + + +def reinit_settings(): + import shutil + + from pytwin.settings import reinit_settings_for_unit_tests + + reinit_settings_for_unit_tests() + if os.path.exists(UNIT_TEST_WD): + shutil.rmtree(UNIT_TEST_WD) + return UNIT_TEST_WD + + +class TestTwinModelLogging: + def test_twin_model_no_logging(self): + # Init unit test + reinit_settings() + # Twin Model does not log anything if PYTWIN_LOGGING_OPT_NOLOGGING + modify_pytwin_logging(new_option=PYTWIN_LOGGING_OPT_NOLOGGING) + log_file = get_pytwin_log_file() + assert log_file is None + twin = TwinModel(model_filepath=COUPLE_CLUTCHES_FILEPATH) + twin.initialize_evaluation() + for i in range(100): + new_inputs = {"Clutch1_in": 1.0 * i / 100, "Clutch2_in": 1.0 * i / 100} + twin.evaluate_step_by_step(step_size=0.01, inputs=new_inputs) + temp_dir = twin.model_temp + assert os.path.exists(temp_dir) + assert len(os.listdir(temp_dir)) == 0 diff --git a/tests/test_settings.py b/tests/test_settings.py index 70b416a5..8c61bab4 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -63,16 +63,21 @@ def test_modify_logging_raises_error(self): assert "Error while setting pytwin logging options!" in str(e) def test_modify_logging_no_logging(self): + from pytwin import TwinModel + from pytwin.twin_runtime.log_level import LogLevel + # Init unit test reinit_settings() assert pytwin_logging_is_enabled() # Disable logging modify_pytwin_logging(new_option=PyTwinLogOption.PYTWIN_LOGGING_OPT_NOLOGGING) + level = TwinModel._get_runtime_log_level() logger = get_pytwin_logger() log_file = get_pytwin_log_file() assert len(logger.handlers) == 0 assert log_file is None assert not pytwin_logging_is_enabled() + assert level == LogLevel.TWIN_NO_LOG def test_modify_logging_console(self): # Init unit test @@ -92,10 +97,16 @@ def test_modify_logging_console(self): assert pytwin_logging_is_enabled() def test_modify_logging_level(self): + from pytwin import TwinModel + from pytwin.settings import get_pytwin_log_level + from pytwin.twin_runtime.log_level import LogLevel + # Init unit test reinit_settings() # Modify logging level works modify_pytwin_logging(new_level=PyTwinLogLevel.PYTWIN_LOG_CRITICAL) + level = get_pytwin_log_level() + runtime_level = TwinModel._get_runtime_log_level() log_file = get_pytwin_log_file() logger = get_pytwin_logger() logger.debug("Hello 10") @@ -106,8 +117,12 @@ def test_modify_logging_level(self): with open(log_file, "r") as f: lines = f.readlines() assert len(lines) == 1 + assert level == PyTwinLogLevel.PYTWIN_LOG_CRITICAL + assert runtime_level == LogLevel.TWIN_LOG_FATAL # Modify logging level can be done dynamically modify_pytwin_logging(new_level=PyTwinLogLevel.PYTWIN_LOG_DEBUG) + level = get_pytwin_log_level() + runtime_level = TwinModel._get_runtime_log_level() logger.debug("Hello 10") logger.info("Hello 20") logger.warning("Hello 30") @@ -116,6 +131,8 @@ def test_modify_logging_level(self): with open(log_file, "r") as f: lines = f.readlines() assert len(lines) == 5 + 1 + assert level == PyTwinLogLevel.PYTWIN_LOG_DEBUG + assert runtime_level == LogLevel.TWIN_LOG_ALL def test_modify_logging_multiple_times(self): # Init unit test