Skip to content

Commit

Permalink
Merge pull request #323 from aadityasinha-dotcom/logger
Browse files Browse the repository at this point in the history
Turning off logger at the class-constructor level
  • Loading branch information
zFernand0 authored Sep 25, 2024
2 parents b00a4c1 + de45309 commit 64bc3ca
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

All notable changes to the Zowe Client Python SDK will be documented in this file.

## Recent Changes

### Enhancements

- Turning of logger at the class-constructor level [#316] (https://github.com/zowe/zowe-client-python-sdk/issues/316)

## `1.0.0-dev21`

### Bug Fixes

- Fixed Core SDK package referencing a non-existent version of Secrets SDK.


## `1.0.0-dev20`

### Enhancements
Expand Down
4 changes: 2 additions & 2 deletions src/core/zowe/core_for_zowe_sdk/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Log:
Attributes
----------
loggers: set
The set of all loggers
dirname: str
Path where the log file is saved
file_handler: logging.FileHandler
Expand All @@ -32,6 +30,8 @@ class Log:
Specifies whether log messages would be saved to a file. True by default.
console_output: bool
Specifies whether log messages would be printed out on console. True by default.
loggers: set
The set of all loggers
"""

dirname: str = os.path.join(os.path.expanduser("~"), ".zowe/logs")
Expand Down
7 changes: 6 additions & 1 deletion src/core/zowe/core_for_zowe_sdk/sdk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ class SdkApi:
Default url used for session
logger_name : str
Name of the logger (same as the filename by default)
log : bool
Flag to disable logger
"""

def __init__(self, profile: dict, default_url: str, logger_name: str = __name__):
def __init__(self, profile: dict, default_url: str, logger_name: str = __name__, log: bool = True):
session = Session(profile)
self.session: ISession = session.load()

self.logger = Log.register_logger(logger_name)

if log == False:
Log.close(self.logger)

self._default_service_url = default_url
self._default_headers = {
"Content-Type": "application/json",
Expand Down
6 changes: 4 additions & 2 deletions src/zos_console/zowe/zos_console_for_zowe_sdk/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class Console(SdkApi):
----------
connection : dict
A profile in dict (json) format
log : bool
Flag to disable logger
"""

def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restconsoles/consoles/defcn", logger_name=__name__)
def __init__(self, connection: dict, log: bool = True):
super().__init__(connection, "/zosmf/restconsoles/consoles/defcn", logger_name=__name__, log=log)

def issue_command(self, command: str, console: Optional[str] = None) -> IssueCommandResponse:
"""Issues a command on z/OS Console.
Expand Down
6 changes: 4 additions & 2 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,12 @@ class Datasets(SdkApi):
----------
connection : dict
A profile for connection in dict (json) format
log : bool
Flag to disable logger
"""

def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__)
def __init__(self, connection: dict, log: bool = True):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__, log=log)
self._default_headers["Accept-Encoding"] = "gzip"

def list(self, name_pattern: str, return_attributes: bool = False) -> DatasetListResponse:
Expand Down
6 changes: 4 additions & 2 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class FileSystems(SdkApi):
----------
connection : dict
A profile for connection in dict (json) format
log : bool
Flag to disable logger
"""

def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__)
def __init__(self, connection: dict, log: bool = True):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__, log=log)
self._default_headers["Accept-Encoding"] = "gzip"

def create(self, file_system_name: str, options: dict = {}) -> dict:
Expand Down
6 changes: 4 additions & 2 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ class Files(SdkApi):
----------
connection: dict
The z/OSMF connection object (generated by the ZoweSDK object)
log : bool
Flag to disable logger
"""

ds: Datasets
uss: USSFiles
fs: FileSystems

def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__)
def __init__(self, connection: dict, log: bool = True):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__, log=log)
self._default_headers["Accept-Encoding"] = "gzip"
self.ds = Datasets(connection)
self.uss = USSFiles(connection)
Expand Down
6 changes: 4 additions & 2 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/uss.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ class USSFiles(SdkApi):
----------
connection: dict
The z/OSMF connection object (generated by the ZoweSDK object)
log : bool
Flag to disable logger
"""

def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__)
def __init__(self, connection: dict, log: bool = True):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__, log=log)
self._default_headers["Accept-Encoding"] = "gzip"

def list(self, path: str) -> USSListResponse:
Expand Down
6 changes: 4 additions & 2 deletions src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class Jobs(SdkApi):
----------
connection : dict
A profile for connection in dict (json) format
log : bool
Flag to disable logger
"""

def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restjobs/jobs/", logger_name=__name__)
def __init__(self, connection: dict, log: bool = True):
super().__init__(connection, "/zosmf/restjobs/jobs/", logger_name=__name__, log=log)

def get_job_status(self, jobname: str, jobid: str) -> JobResponse:
"""
Expand Down
6 changes: 4 additions & 2 deletions src/zos_tso/zowe/zos_tso_for_zowe_sdk/tso.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class Tso(SdkApi):
Connection object
tso_profile: Optional[dict]
Profile used for tso connection
log : bool
Flag to disable logger
"""

def __init__(self, connection: dict, tso_profile: Optional[dict] = None):
super().__init__(connection, "/zosmf/tsoApp/tso", logger_name=__name__)
def __init__(self, connection: dict, tso_profile: Optional[dict] = None, log: bool = True):
super().__init__(connection, "/zosmf/tsoApp/tso", logger_name=__name__, log=log)
self.session_not_found = constants["TsoSessionNotFound"]
self.tso_profile = tso_profile or {}

Expand Down
6 changes: 4 additions & 2 deletions src/zosmf/zowe/zosmf_for_zowe_sdk/zosmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class Zosmf(SdkApi):
----------
connection: dict
The z/OSMF connection object (generated by the ZoweSDK object)
log : bool
Flag to disable logger
"""

def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/info", logger_name=__name__)
def __init__(self, connection: dict, log: bool = True):
super().__init__(connection, "/zosmf/info", logger_name=__name__, log=log)

def get_info(self) -> ZosmfResponse:
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/core/test_sdk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def test_object_should_be_instance_of_class(self):
sdk_api = SdkApi(self.basic_props, self.default_url)
self.assertIsInstance(sdk_api, SdkApi)

def test_object_should_be_instance_with_logger_set_to_false(self):
"""Created object should be instance with logger set to False of SdkApi class."""
sdk_api = SdkApi(self.basic_props, self.default_url, log=False)
self.assertEqual(sdk_api.logger.disabled, True)

@mock.patch("requests.Session.close")
def test_context_manager_closes_session(self, mock_close_request):

Expand Down

0 comments on commit 64bc3ca

Please sign in to comment.