diff options
author | Danglewood <85772166+deeleeramone@users.noreply.github.com> | 2024-11-11 05:34:18 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-11 13:34:18 +0000 |
commit | 804534ed31bbddd8c8ff4b3f7bb12a5ce3455787 (patch) | |
tree | 8ce9774e931b672cd697d43fa4f6b9be9ca27515 | |
parent | baff57dfc889ec60e4d0b0d4c7732db8c2b84e14 (diff) |
[BugFix] Make "logging_suppress" in `SystemSettings` Mutable and Then Actually Suppress Logging Service (#6933)
* make logging_suppress mutable and then actually suppress the logging service
* no startup log if true
* fix tests
* make kwarg truncate bigger
* fix other tests
8 files changed, 72 insertions, 46 deletions
diff --git a/openbb_platform/core/openbb_core/app/logs/handlers_manager.py b/openbb_platform/core/openbb_core/app/logs/handlers_manager.py index 8e79d932f0e..deeaff8d5e5 100644 --- a/openbb_platform/core/openbb_core/app/logs/handlers_manager.py +++ b/openbb_platform/core/openbb_core/app/logs/handlers_manager.py @@ -35,9 +35,9 @@ class HandlersManager: self._add_stderr_handler() elif handler_type == "noop": self._add_noop_handler() - elif handler_type == "file": + elif handler_type == "file" and not self._settings.logging_suppress: self._add_file_handler() - elif handler_type == "posthog": + elif handler_type == "posthog" and not self._settings.logging_suppress: self._add_posthog_handler() else: self._logger.debug("Unknown log handler.") @@ -81,6 +81,9 @@ class HandlersManager: """Update the handlers with new settings.""" logger = self._logger for hdlr in logger.handlers: - if isinstance(hdlr, (PathTrackingFileHandler, PosthogHandler)): + if ( + isinstance(hdlr, (PathTrackingFileHandler, PosthogHandler)) + and not settings.logging_suppress + ): hdlr.settings = settings hdlr.formatter.settings = settings # type: ignore diff --git a/openbb_platform/core/openbb_core/app/logs/logging_service.py b/openbb_platform/core/openbb_core/app/logs/logging_service.py index 9c1900b0c02..076eda4d879 100644 --- a/openbb_platform/core/openbb_core/app/logs/logging_service.py +++ b/openbb_platform/core/openbb_core/app/logs/logging_service.py @@ -217,42 +217,44 @@ class LoggingService(metaclass=SingletonMeta): ) self._handlers_manager.update_handlers(self._logging_settings) - if "login" in route: - self._log_startup(route, custom_headers) - else: - - # Remove CommandContext if any - kwargs.pop("cc", None) - - # Get provider for posthog logs - passed_model = kwargs.get("provider_choices", DummyProvider()) - provider = ( - passed_model.provider - if hasattr(passed_model, "provider") - else "not_passed_to_kwargs" - ) - - # Truncate kwargs if too long - kwargs = {k: str(v)[:100] for k, v in kwargs.items()} - # Get execution info - error = None if all(i is None for i in exec_info) else str(exec_info[1]) - - # Construct message - message_label = "ERROR" if error else "CMD" - log_message = json.dumps( - { - "route": route, - "input": kwargs, - "error": error, - "provider": provider, - "custom_headers": custom_headers, - }, - default=to_jsonable_python, - ) - log_message = f"{message_label}: {log_message}" - log_level = self._logger.error if error else self._logger.info - log_level( - log_message, - extra={"func_name_override": func.__name__}, - exc_info=exec_info, - ) + if not self._logging_settings.logging_suppress: + + if "login" in route: + self._log_startup(route, custom_headers) + else: + + # Remove CommandContext if any + kwargs.pop("cc", None) + + # Get provider for posthog logs + passed_model = kwargs.get("provider_choices", DummyProvider()) + provider = ( + passed_model.provider + if hasattr(passed_model, "provider") + else "not_passed_to_kwargs" + ) + + # Truncate kwargs if too long + kwargs = {k: str(v)[:300] for k, v in kwargs.items()} + # Get execution info + error = None if all(i is None for i in exec_info) else str(exec_info[1]) + + # Construct message + message_label = "ERROR" if error else "CMD" + log_message = json.dumps( + { + "route": route, + "input": kwargs, + "error": error, + "provider": provider, + "custom_headers": custom_headers, + }, + default=to_jsonable_python, + ) + log_message = f"{message_label}: {log_message}" + log_level = self._logger.error if error else self._logger.info + log_level( + log_message, + extra={"func_name_override": func.__name__}, + exc_info=exec_info, + ) diff --git a/openbb_platform/core/openbb_core/app/logs/models/logging_settings.py b/openbb_platform/core/openbb_core/app/logs/models/logging_settings.py index f44ed423c6b..ec8e412db33 100644 --- a/openbb_platform/core/openbb_core/app/logs/models/logging_settings.py +++ b/openbb_platform/core/openbb_core/app/logs/models/logging_settings.py @@ -49,6 +49,8 @@ class LoggingSettings: self.platform: str = system_settings.platform self.python_version: str = system_settings.python_version self.platform_version: str = system_settings.version + self.logging_suppress: bool = system_settings.logging_suppress + self.log_collect: bool = system_settings.log_collect # User self.user_id: Optional[str] = user_id self.user_logs_directory: Path = get_log_dir(user_data_directory) diff --git a/openbb_platform/core/openbb_core/app/service/system_service.py b/openbb_platform/core/openbb_core/app/service/system_service.py index 4d9f9c6feeb..c9e3165e179 100644 --- a/openbb_platform/core/openbb_core/app/service/system_service.py +++ b/openbb_platform/core/openbb_core/app/service/system_service.py @@ -22,6 +22,7 @@ class SystemService(metaclass=SingletonMeta): "api_settings", "python_settings", "debug_mode", + "logging_suppress", } PRO_VALIDATION_HASH = "300ac59fdcc8f899e0bc5c18cda8652220735da1a00e2af365efe9d8e5fe8306" # pragma: allowlist secret diff --git a/openbb_platform/core/tests/app/logs/handlers/test_path_tracking_file_handler.py b/openbb_platform/core/tests/app/logs/handlers/test_path_tracking_file_handler.py index ba81d15e1ab..5e19ff04f6b 100644 --- a/openbb_platform/core/tests/app/logs/handlers/test_path_tracking_file_handler.py +++ b/openbb_platform/core/tests/app/logs/handlers/test_path_tracking_file_handler.py @@ -30,6 +30,8 @@ logging_settings.user_logs_directory.absolute.return_value = Path( ) logging_settings.session_id = "test_session_id" logging_settings.frequency = "H" +logging_settings.logging_suppress = False +logging_settings.log_collect = True @pytest.fixture(scope="module") diff --git a/openbb_platform/core/tests/app/logs/handlers/test_posthog_handler.py b/openbb_platform/core/tests/app/logs/handlers/test_posthog_handler.py index fed708c4f5d..5d2518b36a8 100644 --- a/openbb_platform/core/tests/app/logs/handlers/test_posthog_handler.py +++ b/openbb_platform/core/tests/app/logs/handlers/test_posthog_handler.py @@ -54,6 +54,8 @@ logging_settings.platform = "Windows" logging_settings.python_version = "3.9" logging_settings.platform_version = "1.2.3" logging_settings.user_id = "user123" +logging_settings.logging_suppress = False +logging_settings.log_collect = True @pytest.fixture diff --git a/openbb_platform/core/tests/app/logs/test_handlers_manager.py b/openbb_platform/core/tests/app/logs/test_handlers_manager.py index b2ce9f30321..e34f63adb89 100644 --- a/openbb_platform/core/tests/app/logs/test_handlers_manager.py +++ b/openbb_platform/core/tests/app/logs/test_handlers_manager.py @@ -53,6 +53,8 @@ def test_handlers_added_correctly(): settings = Mock() settings.verbosity = 20 settings.handler_list = ["stdout", "stderr", "noop", "file", "posthog"] + settings.log_collect = True + settings.logging_suppress = False logger = logging.getLogger("test_handlers_added_correctly") handlers_manager = HandlersManager(logger=logger, settings=settings) handlers_manager.setup() diff --git a/openbb_platform/core/tests/app/logs/test_logging_service.py b/openbb_platform/core/tests/app/logs/test_logging_service.py index 1a3bbe93edc..04ec8d097ac 100644 --- a/openbb_platform/core/tests/app/logs/test_logging_service.py +++ b/openbb_platform/core/tests/app/logs/test_logging_service.py @@ -13,6 +13,15 @@ from pydantic import BaseModel # pylint: disable=redefined-outer-name, protected-access +class MockSystemSettings: + """Mock system settings.""" + + def __init__(self): + """Initialize the mock system settings.""" + self.logging_suppress = False + self.log_collect = True + + class MockLoggingSettings: """Mock logging settings.""" @@ -20,6 +29,8 @@ class MockLoggingSettings: """Initialize the mock logging settings.""" self.system_settings = system_settings self.user_settings = user_settings + self.logging_suppress = False + self.log_collect = True class MockOBBject(BaseModel): @@ -32,7 +43,7 @@ class MockOBBject(BaseModel): @pytest.fixture(scope="function") def logging_service(): """Return a LoggingService instance.""" - mock_system_settings = Mock() + mock_system_settings = MockSystemSettings() mock_user_settings = Mock() mock_setup_handlers = Mock() mock_log_startup = Mock() @@ -112,6 +123,7 @@ def test_log_startup(logging_service): credentials=MockCredentials(username="username", password="password"), ) logging_service._system_settings = "your_system_settings" + logging_service._system_settings logging_service._log_startup( route="test_route", custom_headers={"X-OpenBB-Test": "test"} @@ -138,7 +150,7 @@ def test_log_startup(logging_service): [ ( "mock_settings", - "mock_system", + MockSystemSettings(), "mock_route", "mock_func", {}, @@ -149,7 +161,7 @@ def test_log_startup(logging_service): ), ( "mock_settings", - "mock_system", + MockSystemSettings(), "mock_route", "mock_func", {}, @@ -163,7 +175,7 @@ def test_log_startup(logging_service): ), ( "mock_settings", - "mock_system", + MockSystemSettings(), "login", "mock_func", {}, |