Files
semantic-kernel/python/semantic_kernel/utils/null_logger.py
T
Julio Carlos MenendezandEduard van Valkenburg d3dd5c8e7e 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]>
2023-12-16 18:41:41 +00:00

49 lines
1.2 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
from functools import wraps
from logging import Logger, getLogger
from typing import Any, Callable
logger: Logger = getLogger(__name__)
def _nullify(fn) -> Callable[[Any], None]:
"""General wrapper to not call wrapped function"""
@wraps(fn)
def _inner_nullify(*args, **kwargs) -> None:
return
return _inner_nullify
class _NullerMeta(type):
def __new__(cls, classname, base_classes, class_dict):
"""Return a Class that nullifies all Logger object callbacks"""
nullified_dict = {
attr_name: _nullify(attr)
for attr_name, attr in Logger.__dict__.items()
if callable(attr)
}
return type.__new__(
cls, classname, base_classes, {**class_dict, **nullified_dict}
)
class NullLogger(Logger, metaclass=_NullerMeta):
"""
A logger that does nothing.
"""
def __init__(self):
super().__init__(None)
logger.warning(
(
"NullLogger is deprecated and will be removed in a future release,",
"the same goes for all 'log' and 'logger' arguments.",
)
)
__all__ = ["NullLogger"]