Skip to content

Commit

Permalink
sdk/python: logging changes (decouple log config from package)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Koo <[email protected]>
  • Loading branch information
rkoo19 committed Sep 16, 2024
1 parent 056d4f9 commit c95a9fa
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 40 deletions.
3 changes: 0 additions & 3 deletions python/aistore/__init__.py
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 6 additions & 8 deletions python/aistore/sdk/authn/authn_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions python/aistore/sdk/authn/cluster_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
# 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,
HTTP_METHOD_PUT,
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:
Expand Down
15 changes: 7 additions & 8 deletions python/aistore/sdk/authn/role_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions python/aistore/sdk/authn/token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions python/aistore/sdk/authn/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,9 +19,7 @@
URL_PATH_AUTHN_USERS,
)


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger = get_logger(__name__)


class UserManager:
Expand Down
2 changes: 2 additions & 0 deletions python/aistore/sdk/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,5 @@

# Time constants
NANOSECONDS_IN_SECOND = 1_000_000_000

DEFAULT_LOG_FORMAT = "%(asctime)s %(levelname)s: %(message)s"
7 changes: 2 additions & 5 deletions python/aistore/sdk/object_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 29 additions & 1 deletion python/aistore/sdk/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit c95a9fa

Please sign in to comment.