Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set twin_model instance log level at initialisation #118

Open
EDCarman opened this issue Sep 14, 2023 · 0 comments
Open

Add option to set twin_model instance log level at initialisation #118

EDCarman opened this issue Sep 14, 2023 · 0 comments
Labels
enhancement New features or code improvements

Comments

@EDCarman
Copy link
Collaborator

📝 Description of the feature

Currently, when a twin model is instantiated, it takes the global Pytwin log level. The twin runtime is instantiated with log_level=self._get_runtime_log_level().
It would be desirable to have the option to assign log levels on a per-instance basis. This is already possible at the core runtime level.

The current behaviour is problematic in the following case:
Certain twin models can produce extremely verbose logs at levels of INFO or higher, for example, containing detailed solver convergence logs for every time step. These messages quickly overwhelm any log messages from other parts of the code.

One workaround is to use a WARNING or lower log level, but then we lose INFO and DEBUG messages from other parts of the code.

Alternatively, we can use modify_pytwin_logging() to temporarily change the global log level to the desired level before instantiating the twin model e.g.

current_level = pytwin.settings.get_pytwin_log_level()
pytwin.modify_pytwin_logging(new_option=None, new_level='INFO')
twin_model = pytwin.TwinModel(model_path)
pytwin.modify_pytwin_logging(new_option=None, new_level=current_level)

💡 Steps for implementing the feature

We could add an optional log_level argument to the TwinModel __init__() method. This can then be propagated down to _instantiate_twin_model() and use when instantiating TwinRuntime.

The default value for the argument could be set to retain the current behaviour.

Something along the lines of:

class TwinModel(Model):
    ...
    def __init__(self, model_filepath: str, log_level=None):
        ...
        self._instantiate_twin_model(log_level)

    ...
    def _instantiate_twin_model(self, log_level=None):
        ...
        try:
            if log_level is None:
                log_level = self._get_runtime_log_level()
            ...
            # Instantiate twin runtime
            self._twin_runtime = TwinRuntime(
                model_path=self._model_filepath,
                load_model=True,
                log_path=self.model_log,
                log_level=log_level,
            )

🔗 Useful links and references

No response

@EDCarman EDCarman added the enhancement New features or code improvements label Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features or code improvements
Projects
None yet
Development

No branches or pull requests

1 participant