From 897f28cd9328573a7785827cc8520c6a11d7add2 Mon Sep 17 00:00:00 2001 From: Haim Kastner Date: Thu, 18 Apr 2024 12:28:06 +0000 Subject: [PATCH] Fix job error handling & Allow programmatically activating loggers --- README.md | 12 ++++++- .../__init__template.py | 9 ++--- .../core/harmony_endpoint.py | 1 - .../core/job_manager.py | 3 +- .../core/logger.py | 33 +++++++++++++------ setup.py | 2 +- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 241e791..7a48e3d 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ Harmony Endpoint Management SDK uses the official python logger package for logg There are 3 loggers, for general info, errors and to inspect network. -As default they will be disabled, in order to enable logging, set to the `HARMONY_ENDPOINT_SDK_LOGGER` environment variable the following string: +As default they will be disabled, in order to enable logging, set to the `HARMONY_ENDPOINT_SDK_LOGGER` environment variable the following string before loading the SDK: ```bash HARMONY_ENDPOINT_SDK_LOGGER="*" ``` @@ -174,6 +174,16 @@ And for a specific/s logger set the logger name followed by a command as followi HARMONY_ENDPOINT_SDK_LOGGER="info,error,network" ``` +or activate logger programmatically using SDK methods: +```python +from chkp_harmony_endpoint_management_sdk import activate_all_loggers, activate_info_logger, activate_error_logger, activate_network_logger +... +activate_all_loggers() # Will activate all logger +activate_info_logger() # Will activate the info logger only +activate_error_logger() # Will activate the error logger only +activate_network_logger() # Will activate the network logger only +``` + ## 🐞 Report Bug In case of an issue or a bug found in the SDK, please open an [issue](https://github.com/CheckPointSW/harmony-endpoint-management-py-sdk/issues) or report to us [Check Point Software Technologies Ltd](mailto:harmony-endpoint-external-api@checkpoint.com). diff --git a/chkp_harmony_endpoint_management_sdk/__init__template.py b/chkp_harmony_endpoint_management_sdk/__init__template.py index 8f192b4..019c9f1 100644 --- a/chkp_harmony_endpoint_management_sdk/__init__template.py +++ b/chkp_harmony_endpoint_management_sdk/__init__template.py @@ -5,7 +5,7 @@ from chkp_harmony_endpoint_management_sdk.classes.sdk_connection_state import SDKConnectionState from chkp_harmony_endpoint_management_sdk.classes.harmony_endpoint_saas_options import HarmonyEndpointSaaSOptions from chkp_harmony_endpoint_management_sdk.classes.harmony_api_exception import HarmonyApiException, HarmonyErrorScope -from chkp_harmony_endpoint_management_sdk.core.logger import _logger, _error_logger, _network_logger +from chkp_harmony_endpoint_management_sdk.core.logger import activate_all_loggers, activate_info_logger, activate_error_logger, activate_network_logger __all__ = [ @@ -18,9 +18,10 @@ 'OnPremisePortalAuth', 'SDKConnectionState', 'HarmonyEndpointSaaSOptions', - '_logger', - '_error_logger', - '_network_logger', + 'activate_all_loggers', + 'activate_info_logger', + 'activate_error_logger', + 'activate_network_logger', 'HarmonyApiException', 'HarmonyErrorScope' ] \ No newline at end of file diff --git a/chkp_harmony_endpoint_management_sdk/core/harmony_endpoint.py b/chkp_harmony_endpoint_management_sdk/core/harmony_endpoint.py index 2841174..9e5182d 100644 --- a/chkp_harmony_endpoint_management_sdk/core/harmony_endpoint.py +++ b/chkp_harmony_endpoint_management_sdk/core/harmony_endpoint.py @@ -4,7 +4,6 @@ from chkp_harmony_endpoint_management_sdk.classes.infinity_portal_auth import InfinityPortalAuth from chkp_harmony_endpoint_management_sdk.classes.internal import SessionOperations from chkp_harmony_endpoint_management_sdk.classes.on_premise_portal_auth import OnPremisePortalAuth -from chkp_harmony_endpoint_management_sdk.core.logger import _logger, _error_logger, _network_logger from chkp_harmony_endpoint_management_sdk.core.session_manager import SessionManager from chkp_harmony_endpoint_management_sdk.generated.cloud import HarmonyEndpointBase as HarmonyEndpointCloudBase # from chkp_harmony_endpoint_management_sdk.generated.premise import HarmonyEndpointBase as HarmonyEndpointPremiseBase diff --git a/chkp_harmony_endpoint_management_sdk/core/job_manager.py b/chkp_harmony_endpoint_management_sdk/core/job_manager.py index 5890caf..8ac7e13 100644 --- a/chkp_harmony_endpoint_management_sdk/core/job_manager.py +++ b/chkp_harmony_endpoint_management_sdk/core/job_manager.py @@ -42,7 +42,8 @@ def await_for_job(job_id: str, job_status_operation: Callable[[], Any], request_ break if job_status_res.payload["status"] == 'NOT_FOUND' or job_status_res.payload["status"] == 'FAILED': - error_logger(f'Job "{job_id}" failed with status "{job_status_res.payload["status"]}", error: {job_status_res.payload}') + msg = f'Job "{job_id}" failed with status "{job_status_res.payload["status"]}", error: {job_status_res.payload}' + error_logger(msg) raise HarmonyApiException(error_scope=HarmonyErrorScope.SERVICE, request_id=request_id, message=msg, payload_error=job_status_res.payload) logger(f'Job "{job_id}" finished successfully') diff --git a/chkp_harmony_endpoint_management_sdk/core/logger.py b/chkp_harmony_endpoint_management_sdk/core/logger.py index 20fcb8e..5db4513 100644 --- a/chkp_harmony_endpoint_management_sdk/core/logger.py +++ b/chkp_harmony_endpoint_management_sdk/core/logger.py @@ -22,24 +22,37 @@ _error_logger.setLevel(logging.CRITICAL + 1) _network_logger.setLevel(logging.CRITICAL + 1) -if __activate_logs == '*': +logger = _logger.debug +error_logger = _error_logger.error +network_logger = _network_logger.info + +logger(f'logger is activated with "{__activate_logs}"') + +def activate_all_loggers(): + _logger.setLevel(logging.DEBUG) + _error_logger.setLevel(logging.DEBUG) + _network_logger.setLevel(logging.DEBUG) + +def activate_info_logger(): _logger.setLevel(logging.DEBUG) + +def activate_error_logger(): _error_logger.setLevel(logging.DEBUG) + +def activate_network_logger(): _network_logger.setLevel(logging.DEBUG) + + +if __activate_logs == '*': + activate_all_loggers() else: loggers = __activate_logs.split(',') if 'info' in loggers: - _logger.setLevel(logging.DEBUG) + activate_info_logger() if 'error' in loggers: - _error_logger.setLevel(logging.DEBUG) + activate_error_logger() if 'network' in loggers: - _network_logger.setLevel(logging.DEBUG) - -logger = _logger.debug -error_logger = _error_logger.error -network_logger = _network_logger.info - -logger(f'logger is activated with "{__activate_logs}"') + activate_network_logger() \ No newline at end of file diff --git a/setup.py b/setup.py index cf18c45..e49a6c4 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ setup_kwargs = { 'name': "chkp-harmony-endpoint-management-sdk", - 'version': '1.1.31', + 'version': '1.1.33', 'keywords': 'python, harmony, endpoint, sdk, checkpoint', 'license': 'MIT', 'description': 'Harmony Endpoint Official Python SDK',