Skip to content

Commit

Permalink
Fix job error handling & Allow programmatically activating loggers #8
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-haimk authored Apr 18, 2024
2 parents 85f78f3 + 897f28c commit 0d22359
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="*"
```
Expand All @@ -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:[email protected]).
Expand Down
9 changes: 5 additions & 4 deletions chkp_harmony_endpoint_management_sdk/__init__template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand 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'
]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion chkp_harmony_endpoint_management_sdk/core/job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
33 changes: 23 additions & 10 deletions chkp_harmony_endpoint_management_sdk/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0d22359

Please sign in to comment.