From 045e08db6c2f636c8144c470bd587bc202383edd Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Sun, 10 Nov 2024 16:47:26 +0200 Subject: [PATCH 01/20] test logger setup fix --- demisto_sdk/commands/common/logger.py | 1 - 1 file changed, 1 deletion(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index d05f6d433e0..09b8bf8d8d1 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -30,7 +30,6 @@ DEFAULT_FILE_SIZE = 1 * (1024**2) # 1 MB DEFAULT_FILE_COUNT = 10 -global logger logger = loguru.logger # all SDK modules should import from this file, not from loguru logger.disable(None) # enabled at setup_logging() From 2478a4b81c278491a9784261322615d90b0cba73 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Sun, 10 Nov 2024 17:29:10 +0200 Subject: [PATCH 02/20] test logger --- demisto_sdk/commands/common/logger.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index 09b8bf8d8d1..d44c99f3110 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -34,11 +34,11 @@ logger.disable(None) # enabled at setup_logging() -def _setup_neo4j_logger(): - import logging # noqa: TID251 # special case, to control the neo4j logging +# def _setup_neo4j_logger(): +# import logging # noqa: TID251 # special case, to control the neo4j logging - neo4j_log = logging.getLogger("neo4j") - neo4j_log.setLevel(logging.CRITICAL) +# neo4j_log = logging.getLogger("neo4j") +# neo4j_log.setLevel(logging.CRITICAL) def calculate_log_size() -> int: @@ -98,12 +98,12 @@ def logging_setup( In the initial set up there is NO file logging (only console) """ global logger - _setup_neo4j_logger() + # _setup_neo4j_logger() logger.remove(None) # Removes all pre-existing handlers - diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", False)) - colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS), False) + diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) + colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) logger = logger.opt( colors=colorize From b099c45ecc08c6bac7be08c2f18742b2bea0b6a9 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Sun, 10 Nov 2024 18:05:14 +0200 Subject: [PATCH 03/20] test logger --- demisto_sdk/commands/common/logger.py | 58 +++++++++++++-------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index d44c99f3110..dc3a9a34d39 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -34,11 +34,11 @@ logger.disable(None) # enabled at setup_logging() -# def _setup_neo4j_logger(): -# import logging # noqa: TID251 # special case, to control the neo4j logging +def _setup_neo4j_logger(): + import logging # noqa: TID251 # special case, to control the neo4j logging -# neo4j_log = logging.getLogger("neo4j") -# neo4j_log.setLevel(logging.CRITICAL) + neo4j_log = logging.getLogger("neo4j") + neo4j_log.setLevel(logging.CRITICAL) def calculate_log_size() -> int: @@ -98,31 +98,31 @@ def logging_setup( In the initial set up there is NO file logging (only console) """ global logger - # _setup_neo4j_logger() - - logger.remove(None) # Removes all pre-existing handlers - - diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) - colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) - - logger = logger.opt( - colors=colorize - ) # allows using color tags in logs (e.g. logger.info("foo")) - _add_console_logger( - colorize=colorize, threshold=console_threshold, diagnose=diagnose - ) - - if not initial: - _add_file_logger( - log_path=calculate_log_dir(path) / LOG_FILE_NAME, - threshold=file_threshold, - diagnose=diagnose, - ) - os.environ[DEMISTO_SDK_LOGGING_SET] = "true" - - logger.debug( - f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" - ) + _setup_neo4j_logger() + + # logger.remove(None) # Removes all pre-existing handlers + + # diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) + # colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) + + # logger = logger.opt( + # colors=colorize + # ) # allows using color tags in logs (e.g. logger.info("foo")) + # _add_console_logger( + # colorize=colorize, threshold=console_threshold, diagnose=diagnose + # ) + + # if not initial: + # _add_file_logger( + # log_path=calculate_log_dir(path) / LOG_FILE_NAME, + # threshold=file_threshold, + # diagnose=diagnose, + # ) + # os.environ[DEMISTO_SDK_LOGGING_SET] = "true" + + # logger.debug( + # f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" + # ) def _add_file_logger(log_path: Path, threshold: Optional[str], diagnose: bool): From 22888c102b81092a0b2feb2ff464fb75eb77726a Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Sun, 10 Nov 2024 20:49:02 +0200 Subject: [PATCH 04/20] test logger --- demisto_sdk/commands/common/logger.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index dc3a9a34d39..77724ee3f0d 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -100,10 +100,10 @@ def logging_setup( global logger _setup_neo4j_logger() - # logger.remove(None) # Removes all pre-existing handlers + logger.remove(None) # Removes all pre-existing handlers - # diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) - # colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) + diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) + colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) # logger = logger.opt( # colors=colorize From 0fcac1d1681e77267dc4d1f1c9cfdaeef51f917e Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Sun, 10 Nov 2024 21:09:49 +0200 Subject: [PATCH 05/20] test sdk logger --- demisto_sdk/commands/common/logger.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index 77724ee3f0d..940a1e1d513 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -105,9 +105,9 @@ def logging_setup( diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) - # logger = logger.opt( - # colors=colorize - # ) # allows using color tags in logs (e.g. logger.info("foo")) + logger = logger.opt( + colors=colorize + ) # allows using color tags in logs (e.g. logger.info("foo")) # _add_console_logger( # colorize=colorize, threshold=console_threshold, diagnose=diagnose # ) From c96dfd2a8c72e90a47339529ae16f2e4488e2ffe Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Sun, 10 Nov 2024 21:33:48 +0200 Subject: [PATCH 06/20] test logger fix --- demisto_sdk/commands/common/logger.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index 940a1e1d513..c99031ab9b3 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -108,9 +108,9 @@ def logging_setup( logger = logger.opt( colors=colorize ) # allows using color tags in logs (e.g. logger.info("foo")) - # _add_console_logger( - # colorize=colorize, threshold=console_threshold, diagnose=diagnose - # ) + _add_console_logger( + colorize=colorize, threshold=console_threshold, diagnose=diagnose + ) # if not initial: # _add_file_logger( From 0dfb5168af803856415fdc1e4d9722030824079c Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Sun, 10 Nov 2024 21:46:19 +0200 Subject: [PATCH 07/20] test logger --- demisto_sdk/commands/common/logger.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index c99031ab9b3..f0cc4cf0bed 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -100,7 +100,7 @@ def logging_setup( global logger _setup_neo4j_logger() - logger.remove(None) # Removes all pre-existing handlers + logger.remove() # Removes all pre-existing handlers diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) @@ -120,9 +120,9 @@ def logging_setup( # ) # os.environ[DEMISTO_SDK_LOGGING_SET] = "true" - # logger.debug( - # f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" - # ) + logger.debug( + f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" + ) def _add_file_logger(log_path: Path, threshold: Optional[str], diagnose: bool): From 01427400c6228c065fddeb528388e05d2e86dea7 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 10:34:00 +0200 Subject: [PATCH 08/20] test logger --- demisto_sdk/commands/common/logger.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index f0cc4cf0bed..5bf1974ee23 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -105,24 +105,25 @@ def logging_setup( diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) - logger = logger.opt( - colors=colorize - ) # allows using color tags in logs (e.g. logger.info("foo")) + # logger = logger.opt( + # colors=colorize + # ) # allows using color tags in logs (e.g. logger.info("foo")) _add_console_logger( colorize=colorize, threshold=console_threshold, diagnose=diagnose ) # if not initial: - # _add_file_logger( - # log_path=calculate_log_dir(path) / LOG_FILE_NAME, - # threshold=file_threshold, - # diagnose=diagnose, - # ) - # os.environ[DEMISTO_SDK_LOGGING_SET] = "true" - - logger.debug( - f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" - ) + # _add_file_logger( + # log_path=calculate_log_dir(path) / LOG_FILE_NAME, + # threshold=file_threshold, + # diagnose=diagnose, + # ) + os.environ[DEMISTO_SDK_LOGGING_SET] = "true" + logger.info('BARRY BARRY') + logger.debug('YOSI YOSI') + # logger.debug( + # f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" + # ) def _add_file_logger(log_path: Path, threshold: Optional[str], diagnose: bool): From 6fe8ab45ec3f8aec7f2b5559c4a4e7469efa244b Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 11:48:01 +0200 Subject: [PATCH 09/20] test-logger --- demisto_sdk/commands/common/logger.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index 5bf1974ee23..da07d1c0ea5 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -5,7 +5,7 @@ from typing import Iterable, Optional, Union import loguru # noqa: TID251 # This is the only place where we allow it - +import logging from demisto_sdk.commands.common.constants import ( DEMISTO_SDK_LOG_FILE_PATH, DEMISTO_SDK_LOG_FILE_SIZE, @@ -34,6 +34,11 @@ logger.disable(None) # enabled at setup_logging() +class PropagateHandler(logging.Handler): + def emit(self, record: logging.LogRecord) -> None: + logging.getLogger(record.name).handle(record) + + def _setup_neo4j_logger(): import logging # noqa: TID251 # special case, to control the neo4j logging @@ -105,12 +110,14 @@ def logging_setup( diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) + logger.add(PropagateHandler(), format="{message}") + # logger = logger.opt( # colors=colorize # ) # allows using color tags in logs (e.g. logger.info("foo")) - _add_console_logger( - colorize=colorize, threshold=console_threshold, diagnose=diagnose - ) + # _add_console_logger( + # colorize=colorize, threshold=console_threshold, diagnose=diagnose + # ) # if not initial: # _add_file_logger( @@ -152,6 +159,7 @@ def _add_console_logger(colorize: bool, threshold: Optional[str], diagnose: bool ) + def log_system_details(): logger.debug(f"Platform: {platform.system()}") logger.debug(f"Python version: {sys.version}") From d7fad7eb4e445c33b2a30fe1b47329208caea437 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 13:25:00 +0200 Subject: [PATCH 10/20] test logger propagation --- demisto_sdk/commands/common/logger.py | 38 ++++++++++++++------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index da07d1c0ea5..b632729e665 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -97,6 +97,7 @@ def logging_setup( file_threshold: str = DEFAULT_FILE_THRESHOLD, path: Optional[Union[Path, str]] = None, initial: bool = False, + propagate: bool = False ): """ The initial set up is required since we have code (e.g. get_content_path) that runs in __main__ before the typer/click commands set up the logger. @@ -110,27 +111,28 @@ def logging_setup( diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) - logger.add(PropagateHandler(), format="{message}") - - # logger = logger.opt( - # colors=colorize - # ) # allows using color tags in logs (e.g. logger.info("foo")) - # _add_console_logger( - # colorize=colorize, threshold=console_threshold, diagnose=diagnose - # ) - - # if not initial: - # _add_file_logger( - # log_path=calculate_log_dir(path) / LOG_FILE_NAME, - # threshold=file_threshold, - # diagnose=diagnose, - # ) + if propagate: + logger.add(PropagateHandler(), format=calling_function+"-{message}") + else: + + logger = logger.opt( + colors=colorize + ) # allows using color tags in logs (e.g. logger.info("foo")) + _add_console_logger( + colorize=colorize, threshold=console_threshold, diagnose=diagnose + ) + if not initial: + _add_file_logger( + log_path=calculate_log_dir(path) / LOG_FILE_NAME, + threshold=file_threshold, + diagnose=diagnose, + ) os.environ[DEMISTO_SDK_LOGGING_SET] = "true" logger.info('BARRY BARRY') logger.debug('YOSI YOSI') - # logger.debug( - # f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" - # ) + logger.debug( + f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" + ) def _add_file_logger(log_path: Path, threshold: Optional[str], diagnose: bool): From 120d1c8fd56938eee14833e7c827a66a2a3edf56 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 13:36:04 +0200 Subject: [PATCH 11/20] test logger --- demisto_sdk/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demisto_sdk/__init__.py b/demisto_sdk/__init__.py index 7f505736095..245bd0e3413 100644 --- a/demisto_sdk/__init__.py +++ b/demisto_sdk/__init__.py @@ -1,3 +1,3 @@ from demisto_sdk.commands.common.logger import logging_setup -logging_setup(initial=True, calling_function="__init__") +# logging_setup(initial=True, calling_function="__init__") From d75b11d28e467ad00021f7ccfa48c97929428ed1 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 13:36:20 +0200 Subject: [PATCH 12/20] test logger --- demisto_sdk/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demisto_sdk/__init__.py b/demisto_sdk/__init__.py index 245bd0e3413..4054b9db3b7 100644 --- a/demisto_sdk/__init__.py +++ b/demisto_sdk/__init__.py @@ -1,3 +1,3 @@ -from demisto_sdk.commands.common.logger import logging_setup +# from demisto_sdk.commands.common.logger import logging_setup # logging_setup(initial=True, calling_function="__init__") From e24fb78fad7a82cfc36a9541e59359e928dfd80f Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 14:35:33 +0200 Subject: [PATCH 13/20] propagate logger --- demisto_sdk/__init__.py | 6 +++--- demisto_sdk/commands/common/logger.py | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/demisto_sdk/__init__.py b/demisto_sdk/__init__.py index 4054b9db3b7..5b1158ed819 100644 --- a/demisto_sdk/__init__.py +++ b/demisto_sdk/__init__.py @@ -1,3 +1,3 @@ -# from demisto_sdk.commands.common.logger import logging_setup - -# logging_setup(initial=True, calling_function="__init__") +if __name__ == "__main__": + from demisto_sdk.commands.common.logger import logging_setup + logging_setup(initial=True, calling_function="__init__") diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index b632729e665..d8a8983b7dd 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -101,7 +101,16 @@ def logging_setup( ): """ The initial set up is required since we have code (e.g. get_content_path) that runs in __main__ before the typer/click commands set up the logger. - In the initial set up there is NO file logging (only console) + In the initial set up there is NO file logging (only console). + + Parameters: + - calling_function (str): The name of the function invoking the logger setup, included in all logs. + - console_threshold (str): Log level for console output. + - file_threshold (str): Log level for file output. + - path (Union[Path, str], optional): Path for file logs. If None, defaults to the calculated log directory. + - initial (bool): Indicates if the setup is for the initial configuration (console-only if True). + - propagate (bool): If True, propagates logs to Python's logging system. + """ global logger _setup_neo4j_logger() @@ -112,7 +121,7 @@ def logging_setup( colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) if propagate: - logger.add(PropagateHandler(), format=calling_function+"-{message}") + _propagate_logger(console_threshold) else: logger = logger.opt( @@ -128,12 +137,20 @@ def logging_setup( diagnose=diagnose, ) os.environ[DEMISTO_SDK_LOGGING_SET] = "true" - logger.info('BARRY BARRY') - logger.debug('YOSI YOSI') logger.debug( f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" ) +def _propagate_logger(threshold: Optional[str],): + """ + Adds a PropagateHandler to Loguru's logger to forward logs to Python's logging system. + """ + logger.add( + PropagateHandler(), + format=CONSOLE_FORMAT, + level=(threshold or DEFAULT_CONSOLE_THRESHOLD), + ) + def _add_file_logger(log_path: Path, threshold: Optional[str], diagnose: bool): logger.add( From 11c8f6a0a88f5aea6675f91f8ca2d43296b7b29e Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 14:37:01 +0200 Subject: [PATCH 14/20] merge master --- demisto_sdk/commands/common/logger.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index d8a8983b7dd..c3dd5620ebf 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -1,3 +1,4 @@ +import logging import os import platform import sys @@ -5,7 +6,7 @@ from typing import Iterable, Optional, Union import loguru # noqa: TID251 # This is the only place where we allow it -import logging + from demisto_sdk.commands.common.constants import ( DEMISTO_SDK_LOG_FILE_PATH, DEMISTO_SDK_LOG_FILE_SIZE, From cb05eb696c25747bbdfa359bb2d3c8eac4fde92b Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 17:39:58 +0200 Subject: [PATCH 15/20] changelog --- .changelog/4657.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .changelog/4657.yml diff --git a/.changelog/4657.yml b/.changelog/4657.yml new file mode 100644 index 00000000000..e26f0eb3c63 --- /dev/null +++ b/.changelog/4657.yml @@ -0,0 +1,4 @@ +changes: +- description: Fixing logger crashes when imported as library in content scripts/integrations. + type: fix +pr_number: 4657 From af445221da00c3e8b37ffbb793db36faa9156dc7 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Mon, 11 Nov 2024 17:50:10 +0200 Subject: [PATCH 16/20] pre-commit fixes --- demisto_sdk/__init__.py | 1 + demisto_sdk/commands/common/logger.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/demisto_sdk/__init__.py b/demisto_sdk/__init__.py index 5b1158ed819..5bfed9e78f4 100644 --- a/demisto_sdk/__init__.py +++ b/demisto_sdk/__init__.py @@ -1,3 +1,4 @@ if __name__ == "__main__": from demisto_sdk.commands.common.logger import logging_setup + logging_setup(initial=True, calling_function="__init__") diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index c3dd5620ebf..b957095d136 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -1,4 +1,4 @@ -import logging +import logging # noqa: TID251 # Required for propagation handling. import os import platform import sys @@ -98,7 +98,7 @@ def logging_setup( file_threshold: str = DEFAULT_FILE_THRESHOLD, path: Optional[Union[Path, str]] = None, initial: bool = False, - propagate: bool = False + propagate: bool = False, ): """ The initial set up is required since we have code (e.g. get_content_path) that runs in __main__ before the typer/click commands set up the logger. @@ -118,13 +118,12 @@ def logging_setup( logger.remove() # Removes all pre-existing handlers - diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", 'False')) - colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, 'False')) + diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", "False")) + colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, "False")) if propagate: _propagate_logger(console_threshold) else: - logger = logger.opt( colors=colorize ) # allows using color tags in logs (e.g. logger.info("foo")) @@ -142,7 +141,10 @@ def logging_setup( f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" ) -def _propagate_logger(threshold: Optional[str],): + +def _propagate_logger( + threshold: Optional[str], +): """ Adds a PropagateHandler to Loguru's logger to forward logs to Python's logging system. """ @@ -179,7 +181,6 @@ def _add_console_logger(colorize: bool, threshold: Optional[str], diagnose: bool ) - def log_system_details(): logger.debug(f"Platform: {platform.system()}") logger.debug(f"Python version: {sys.version}") From a55cdb3ca981e7313e479360993385275ce4379c Mon Sep 17 00:00:00 2001 From: barryyosi-panw <158817412+barryyosi-panw@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:08:48 +0200 Subject: [PATCH 17/20] Update .changelog/4657.yml Co-authored-by: dorschw <81086590+dorschw@users.noreply.github.com> --- .changelog/4657.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/4657.yml b/.changelog/4657.yml index e26f0eb3c63..f71a3cd93a3 100644 --- a/.changelog/4657.yml +++ b/.changelog/4657.yml @@ -1,4 +1,4 @@ changes: -- description: Fixing logger crashes when imported as library in content scripts/integrations. +- description: Fixing logger crashes when imported as library. type: fix pr_number: 4657 From 5756bd0b1ab9f988f206abdcf4c95ca9a4f91b15 Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Tue, 12 Nov 2024 15:10:47 +0200 Subject: [PATCH 18/20] update possible executing module names --- demisto_sdk/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demisto_sdk/__init__.py b/demisto_sdk/__init__.py index 5bfed9e78f4..91da2ab1cf9 100644 --- a/demisto_sdk/__init__.py +++ b/demisto_sdk/__init__.py @@ -1,4 +1,4 @@ -if __name__ == "__main__": - from demisto_sdk.commands.common.logger import logging_setup +if __name__ in ["__main__", 'demisto_sdk']: + from demisto_sdk.commands.common.logger import logging_setup logging_setup(initial=True, calling_function="__init__") From 9a4b897a3fa7f33c337b8052f62e37d297691a5a Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Tue, 12 Nov 2024 15:21:17 +0200 Subject: [PATCH 19/20] pre commit fix --- demisto_sdk/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demisto_sdk/__init__.py b/demisto_sdk/__init__.py index 91da2ab1cf9..5bf2275f6b7 100644 --- a/demisto_sdk/__init__.py +++ b/demisto_sdk/__init__.py @@ -1,4 +1,4 @@ - -if __name__ in ["__main__", 'demisto_sdk']: +if __name__ in ["__main__", "demisto_sdk"]: from demisto_sdk.commands.common.logger import logging_setup + logging_setup(initial=True, calling_function="__init__") From 8988e3ae2e51810d180fb118f19274c4a512a54b Mon Sep 17 00:00:00 2001 From: barryyosi-panw Date: Tue, 12 Nov 2024 15:45:23 +0200 Subject: [PATCH 20/20] constant introduction --- demisto_sdk/commands/common/logger.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/demisto_sdk/commands/common/logger.py b/demisto_sdk/commands/common/logger.py index b957095d136..2ac619f63c0 100644 --- a/demisto_sdk/commands/common/logger.py +++ b/demisto_sdk/commands/common/logger.py @@ -31,6 +31,8 @@ DEFAULT_FILE_SIZE = 1 * (1024**2) # 1 MB DEFAULT_FILE_COUNT = 10 +LOGURU_DIAGNOSE = "LOGURU_DIAGNOSE" + logger = loguru.logger # all SDK modules should import from this file, not from loguru logger.disable(None) # enabled at setup_logging() @@ -118,7 +120,7 @@ def logging_setup( logger.remove() # Removes all pre-existing handlers - diagnose = string_to_bool(os.getenv("LOGURU_DIAGNOSE", "False")) + diagnose = string_to_bool(os.getenv(LOGURU_DIAGNOSE, "False")) colorize = not string_to_bool(os.getenv(DEMISTO_SDK_LOG_NO_COLORS, "False")) if propagate: @@ -136,7 +138,7 @@ def logging_setup( threshold=file_threshold, diagnose=diagnose, ) - os.environ[DEMISTO_SDK_LOGGING_SET] = "true" + os.environ[DEMISTO_SDK_LOGGING_SET] = "true" logger.debug( f"logger setup: {calling_function=},{console_threshold=},{file_threshold=},{path=},{initial=}" )