Python: Uses Python standard logging (#4115)

### Motivation and Context

This replaces passing Logger instances as arguments around with the
preferred Python way of having each module define a module-wide `logger`
instance at the top of the module. Fixes #3711

### Description

Removes any `log` argument and argument documentation. Replaces usage of
those `log` arguments with the module-wide `logger` instance.

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Eduard van Valkenburg <[email protected]>
This commit is contained in:
Julio Carlos Menendez
2023-12-16 18:41:41 +00:00
committed by GitHub
co-authored by Eduard van Valkenburg
parent 24a099965e
commit d3dd5c8e7e
78 changed files with 913 additions and 788 deletions
@@ -1,31 +1,36 @@
# Copyright (c) Microsoft. All rights reserved.
import logging
from typing import Awaitable, Callable, TypeVar
from typing import Any, Awaitable, Callable, Optional, TypeVar
from semantic_kernel.reliability.retry_mechanism_base import RetryMechanismBase
from semantic_kernel.sk_pydantic import SKBaseModel
T = TypeVar("T")
logger: logging.Logger = logging.getLogger(__name__)
class PassThroughWithoutRetry(RetryMechanismBase, SKBaseModel):
"""A retry mechanism that does not retry."""
async def execute_with_retry_async(
self, action: Callable[[], Awaitable[T]], log: logging.Logger
self, action: Callable[[], Awaitable[T]], log: Optional[Any] = None
) -> Awaitable[T]:
"""Executes the given action with retry logic.
Arguments:
action {Callable[[], Awaitable[T]]} -- The action to retry on exception.
log {logging.Logger} -- The logger to use.
Returns:
Awaitable[T] -- An awaitable that will return the result of the action.
"""
if log:
logger.warning(
"The `log` parameter is deprecated. Please use the `logging` module instead."
)
try:
await action()
except Exception as e:
log.warning(e, "Error executing action, not retrying")
logger.warning(e, "Error executing action, not retrying")
raise
@@ -2,23 +2,28 @@
import logging
from abc import ABC, abstractmethod
from typing import Awaitable, Callable, TypeVar
from typing import Any, Awaitable, Callable, Optional, TypeVar
T = TypeVar("T")
logger: logging.Logger = logging.getLogger(__name__)
class RetryMechanismBase(ABC):
@abstractmethod
async def execute_with_retry_async(
self, action: Callable[[], Awaitable[T]], log: logging.Logger
self, action: Callable[[], Awaitable[T]], log: Optional[Any] = None
) -> Awaitable[T]:
"""Executes the given action with retry logic.
Arguments:
action {Callable[[], Awaitable[T]]} -- The action to retry on exception.
log {logging.Logger} -- The logger to use.
Returns:
Awaitable[T] -- An awaitable that will return the result of the action.
"""
if log:
logger.warning(
"The `log` parameter is deprecated. Please use the `logging` module instead."
)
pass