From c95a9fa4b0461f1a90fae814451c801c950b9c4a Mon Sep 17 00:00:00 2001 From: Ryan Koo Date: Wed, 11 Sep 2024 16:14:36 -0400 Subject: [PATCH] sdk/python: logging changes (decouple log config from package) Signed-off-by: Ryan Koo --- python/aistore/__init__.py | 3 --- python/aistore/sdk/authn/authn_client.py | 14 +++++----- python/aistore/sdk/authn/cluster_manager.py | 8 +++--- python/aistore/sdk/authn/role_manager.py | 15 +++++------ python/aistore/sdk/authn/token_manager.py | 8 +++--- python/aistore/sdk/authn/user_manager.py | 7 ++--- python/aistore/sdk/const.py | 2 ++ python/aistore/sdk/object_file.py | 7 ++--- python/aistore/sdk/utils.py | 30 ++++++++++++++++++++- 9 files changed, 54 insertions(+), 40 deletions(-) diff --git a/python/aistore/__init__.py b/python/aistore/__init__.py index c606c05215..58d8f3349a 100644 --- a/python/aistore/__init__.py +++ b/python/aistore/__init__.py @@ -1,5 +1,2 @@ -import logging from aistore.sdk.client import Client from aistore.version import __version__ - -logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s: %(message)s") diff --git a/python/aistore/sdk/authn/authn_client.py b/python/aistore/sdk/authn/authn_client.py index 293a2946df..c93f0a08ce 100644 --- a/python/aistore/sdk/authn/authn_client.py +++ b/python/aistore/sdk/authn/authn_client.py @@ -2,25 +2,23 @@ # Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. # -import logging from typing import Optional, Tuple, Union from urllib3 import Retry from aistore.sdk.request_client import RequestClient -from aistore.sdk.const import ( - HTTP_METHOD_POST, - URL_PATH_AUTHN_USERS, -) from aistore.sdk.session_manager import SessionManager +from aistore.sdk.utils import get_logger from aistore.sdk.authn.types import TokenMsg, LoginMsg from aistore.sdk.authn.cluster_manager import ClusterManager from aistore.sdk.authn.role_manager import RoleManager from aistore.sdk.authn.token_manager import TokenManager from aistore.sdk.authn.user_manager import UserManager from aistore.sdk.authn.utils import raise_authn_error +from aistore.sdk.const import ( + HTTP_METHOD_POST, + URL_PATH_AUTHN_USERS, +) -# logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) # pylint: disable=too-many-arguments, too-few-public-methods diff --git a/python/aistore/sdk/authn/cluster_manager.py b/python/aistore/sdk/authn/cluster_manager.py index 3a362a810b..6565eb2434 100644 --- a/python/aistore/sdk/authn/cluster_manager.py +++ b/python/aistore/sdk/authn/cluster_manager.py @@ -2,10 +2,11 @@ # Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. # -import logging from typing import List, Optional from aistore.sdk.request_client import RequestClient from aistore.sdk.client import Client as AISClient +from aistore.sdk.authn.types import ClusterInfo, ClusterList +from aistore.sdk.utils import get_logger from aistore.sdk.const import ( HTTP_METHOD_GET, HTTP_METHOD_POST, @@ -13,11 +14,8 @@ HTTP_METHOD_DELETE, URL_PATH_AUTHN_CLUSTERS, ) -from aistore.sdk.authn.types import ClusterInfo, ClusterList -# logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class ClusterManager: diff --git a/python/aistore/sdk/authn/role_manager.py b/python/aistore/sdk/authn/role_manager.py index 08f2982b3f..b9219cb74a 100644 --- a/python/aistore/sdk/authn/role_manager.py +++ b/python/aistore/sdk/authn/role_manager.py @@ -4,9 +4,14 @@ # pylint: disable=too-many-arguments, duplicate-code -import logging from typing import List from aistore.sdk.request_client import RequestClient +from aistore.sdk.authn.access_attr import AccessAttr +from aistore.sdk.authn.cluster_manager import ClusterManager +from aistore.sdk.types import BucketModel +from aistore.sdk.namespace import Namespace +from aistore.sdk.authn.errors import ErrRoleNotFound +from aistore.sdk.utils import get_logger from aistore.sdk.const import ( HTTP_METHOD_GET, HTTP_METHOD_POST, @@ -20,14 +25,8 @@ BucketPermission, ClusterPermission, ) -from aistore.sdk.authn.access_attr import AccessAttr -from aistore.sdk.authn.cluster_manager import ClusterManager -from aistore.sdk.types import BucketModel -from aistore.sdk.namespace import Namespace -from aistore.sdk.authn.errors import ErrRoleNotFound -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class RoleManager: diff --git a/python/aistore/sdk/authn/token_manager.py b/python/aistore/sdk/authn/token_manager.py index 915636d591..7521003221 100644 --- a/python/aistore/sdk/authn/token_manager.py +++ b/python/aistore/sdk/authn/token_manager.py @@ -2,17 +2,15 @@ # Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. # -import logging from aistore.sdk.request_client import RequestClient +from aistore.sdk.authn.types import TokenMsg +from aistore.sdk.utils import get_logger from aistore.sdk.const import ( HTTP_METHOD_DELETE, URL_PATH_AUTHN_TOKENS, ) -from aistore.sdk.authn.types import TokenMsg - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class TokenManager: # pylint: disable=duplicate-code diff --git a/python/aistore/sdk/authn/user_manager.py b/python/aistore/sdk/authn/user_manager.py index c2fae2a72c..1d51d499c3 100644 --- a/python/aistore/sdk/authn/user_manager.py +++ b/python/aistore/sdk/authn/user_manager.py @@ -4,14 +4,13 @@ # pylint: disable=too-many-arguments, duplicate-code -import logging from typing import List, Optional - from aistore.sdk.authn.types import UserInfo, RolesList, UsersList from aistore.sdk.authn.errors import ErrUserNotFound from aistore.sdk.request_client import RequestClient from aistore.sdk.authn.role_manager import RoleManager +from aistore.sdk.utils import get_logger from aistore.sdk.const import ( HTTP_METHOD_DELETE, HTTP_METHOD_GET, @@ -20,9 +19,7 @@ URL_PATH_AUTHN_USERS, ) - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class UserManager: diff --git a/python/aistore/sdk/const.py b/python/aistore/sdk/const.py index c0ec5823ab..5baeb08a59 100644 --- a/python/aistore/sdk/const.py +++ b/python/aistore/sdk/const.py @@ -164,3 +164,5 @@ # Time constants NANOSECONDS_IN_SECOND = 1_000_000_000 + +DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)s: %(message)s" diff --git a/python/aistore/sdk/object_file.py b/python/aistore/sdk/object_file.py index 544b1cca23..73d4523aec 100644 --- a/python/aistore/sdk/object_file.py +++ b/python/aistore/sdk/object_file.py @@ -4,16 +4,13 @@ # pylint: disable=too-many-branches -import logging from io import BufferedIOBase from typing import Iterator - import requests - from aistore.sdk.object_reader import ObjectReader +from aistore.sdk.utils import get_logger -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) +logger = get_logger(__name__) class SimpleBuffer: diff --git a/python/aistore/sdk/utils.py b/python/aistore/sdk/utils.py index e498b02298..0a671462cb 100644 --- a/python/aistore/sdk/utils.py +++ b/python/aistore/sdk/utils.py @@ -1,6 +1,8 @@ # # Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved. # + +import logging from pathlib import Path from typing import Callable, Iterator, Tuple, Type, TypeVar from urllib.parse import urlparse @@ -14,7 +16,12 @@ from pydantic import BaseModel, parse_raw_as from requests import Response -from aistore.sdk.const import UTF_ENCODING, HEADER_CONTENT_TYPE, MSGPACK_CONTENT_TYPE +from aistore.sdk.const import ( + UTF_ENCODING, + HEADER_CONTENT_TYPE, + MSGPACK_CONTENT_TYPE, + DEFAULT_LOG_FORMAT, +) from aistore.sdk.errors import ( AISError, ErrBckNotFound, @@ -214,3 +221,24 @@ def parse_url(url: str) -> Tuple[str, str, str]: parsed_url = urlparse(url) path = parsed_url.path.lstrip("/") return parsed_url.scheme, parsed_url.netloc, path + + +def get_logger(name: str, log_format: str = DEFAULT_LOG_FORMAT): + """ + Create or retrieve a logger with the specified configuration. + + Args: + name (str): The name of the logger. + format (str, optional): Logging format. + + Returns: + logging.Logger: Configured logger instance. + """ + logger = logging.getLogger(name) + if not logger.hasHandlers(): + handler = logging.StreamHandler() + formatter = logging.Formatter(log_format) + handler.setFormatter(formatter) + logger.addHandler(handler) + logger.propagate = False + return logger