From 7a5c4e9a2bcaec43db8c81f2e178ff1abb1a5240 Mon Sep 17 00:00:00 2001 From: Massimo Capodiferro <77293250+maxcapodi78@users.noreply.github.com> Date: Fri, 19 Apr 2024 16:08:53 +0200 Subject: [PATCH] FIX: Logger (#392) * Fix duplication of logger when Pyaedt logger is initialized. * Fix duplication of logger when Pyaedt logger is initialized. * MISC: Auto fixes from pre-commit.com hooks For more information, see https://pre-commit.ci * Fix duplication of logger when Pyaedt logger is initialized. * MISC: Auto fixes from pre-commit.com hooks For more information, see https://pre-commit.ci * Fix duplication of logger when Pyaedt logger is initialized. * MISC: Auto fixes from pre-commit.com hooks For more information, see https://pre-commit.ci --------- Co-authored-by: maxcapodi78 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/pyedb/dotnet/edb_core/dotnet/database.py | 9 ++++--- src/pyedb/edb_logger.py | 16 ++++++++++- src/pyedb/generic/settings.py | 28 ++++++++++++++------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/pyedb/dotnet/edb_core/dotnet/database.py b/src/pyedb/dotnet/edb_core/dotnet/database.py index b2ba29945c..0fef1ecca0 100644 --- a/src/pyedb/dotnet/edb_core/dotnet/database.py +++ b/src/pyedb/dotnet/edb_core/dotnet/database.py @@ -721,10 +721,11 @@ def __init__(self, edbversion, student_version=False): """Initialize DLLs.""" from pyedb.dotnet.clr_module import _clr, edb_initialized - if settings.enable_screen_logs: - self._logger.enable_stdout_log() - else: # pragma: no cover - self._logger.disable_stdout_log() + if not settings.use_pyaedt_log: + if settings.enable_screen_logs: + self._logger.enable_stdout_log() + else: # pragma: no cover + self._logger.disable_stdout_log() if not edb_initialized: # pragma: no cover self._logger.error("Failed to initialize Dlls.") return diff --git a/src/pyedb/edb_logger.py b/src/pyedb/edb_logger.py index 608d2f9d1b..9e821e2949 100644 --- a/src/pyedb/edb_logger.py +++ b/src/pyedb/edb_logger.py @@ -415,4 +415,18 @@ def glb(self): return self._global -pyedb_logger = EdbLogger(to_stdout=settings.enable_screen_logs) +logger = logging.getLogger("Global") +if any("aedt_logger" in str(i) for i in logger.filters): + from pyaedt.generic.settings import settings as pyaedt_settings + + from pyedb.generic.settings import settings as pyaedb_settings + + pyedb_logger = pyaedt_settings.logger + pyaedb_settings.use_pyaedt_log = True + pyaedb_settings.logger = pyedb_logger + +else: + pyedb_logger = EdbLogger(to_stdout=settings.enable_screen_logs) + from pyedb.generic.settings import settings as pyaedb_settings + + pyaedb_settings.logger = pyedb_logger diff --git a/src/pyedb/generic/settings.py b/src/pyedb/generic/settings.py index 064d9ac225..4c1f151a7c 100644 --- a/src/pyedb/generic/settings.py +++ b/src/pyedb/generic/settings.py @@ -59,6 +59,26 @@ def __init__(self): self._global_log_file_size = 10 self._lsf_queue = None self._edb_environment_variables = {} + self._use_pyaedt_log = False + self._logger = None + + @property + def logger(self): + """Active logger.""" + return self._logger + + @logger.setter + def logger(self, val): + self._logger = val + + @property + def use_pyaedt_log(self): + """Flag that disable Edb log when PyAEDT is used.""" + return self._use_pyaedt_log + + @use_pyaedt_log.setter + def use_pyaedt_log(self, value): + self._use_pyaedt_log = value @property def edb_environment_variables(self): @@ -130,14 +150,6 @@ def enable_debug_methods_argument_logger(self): def enable_debug_methods_argument_logger(self, val): self._enable_debug_methods_argument_logger = val - @property - def logger(self): - """Active logger.""" - try: - return logging.getLogger("Global") - except: # pragma: no cover - return logging.getLogger(__name__) - @property def enable_error_handler(self): """Flag for enabling and disabling the internal PyEDB error handling."""