Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup deprecation logs #164

Merged
merged 6 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions ovos_utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import functools
import inspect
import logging
import os
Expand Down Expand Up @@ -175,22 +176,25 @@ def init_service_logger(service_name):
def log_deprecation(log_message: str = "DEPRECATED",
deprecation_version: str = "Unknown",
func_name: str = None,
func_module: str = None,
excluded_package_refs: List[str] = None):
"""
Log a deprecation warning with information for the call outside the module
that is generating the warning
@param log_message: Log contents describing the deprecation
@param deprecation_version: package version in which method will be deprecated
@param func_name: decorated function name (else read from stack)
@param func_module: decorated function module (else read from stack)
@param excluded_package_refs: list of packages to exclude from call origin
determination. i.e. an internal exception handling method should log the
first call external to that package
"""
import inspect
stack = inspect.stack()[1:] # [0] is this method
call_info = "Unknown Origin"
origin_module = None
log_name = LOG.name
origin_module = func_module
log_name = f"{LOG.name} - {func_module}:{func_name}" if \
func_module and func_name else LOG.name
for call in stack:
module = inspect.getmodule(call.frame)
name = module.__name__ if module else call.filename
Expand All @@ -199,6 +203,7 @@ def log_deprecation(log_message: str = "DEPRECATED",
# Skip calls from this module and unittests to get at real origin
continue
if not origin_module:
# Assume first outside call is the origin if not specified
origin_module = name
log_name = f"{LOG.name} - {name}:{func_name or call[3]}:{call[2]}"
continue
Expand All @@ -221,9 +226,13 @@ def deprecated(log_message: str, deprecation_version: str):
@param deprecation_version: package version in which deprecation will occur
"""
def wrapped(func):
log_deprecation(log_message=log_message,
func_name=func.__name__,
deprecation_version=deprecation_version)
return func
@functools.wraps(func)
def log_wrapper(*args, **kwargs):
log_deprecation(log_message=log_message,
func_name=func.__qualname__,
func_module=func.__module__,
deprecation_version=deprecation_version)
return func(*args, **kwargs)
return log_wrapper

return wrapped
2 changes: 1 addition & 1 deletion ovos_utils/skills/audioservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class AudioServiceInterface(ClassicAudioServiceInterface):
"""

@deprecated("AudioServiceInterface has been deprecated, compatibility "
"layer in use\nplease move to OCPInterface", "0.1.0")
"layer in use. please move to OCPInterface", "0.1.0")
def __init__(self, bus=None):
super().__init__(bus)

Expand Down
6 changes: 6 additions & 0 deletions test/unittests/deprecation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
@deprecated("imported deprecation", "0.1.0")
def deprecated_function():
pass


class Deprecated:
@deprecated("Class Deprecated", "0.2.0")
def __init__(self):
pass
9 changes: 7 additions & 2 deletions test/unittests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ def test_deprecated_decorator(self, create_logger):
from ovos_utils.log import deprecated
import sys
sys.path.insert(0, dirname(__file__))
from deprecation_helper import deprecated_function
from deprecation_helper import deprecated_function, Deprecated
deprecated_function()
log_warning.assert_called_once()
log_msg = log_warning.call_args[0][0]
self.assertIn('version=0.1.0', log_msg, log_msg)
self.assertIn('test_log:', log_msg, log_msg)
self.assertIn('test_log', log_msg, log_msg)
self.assertIn('imported deprecation', log_msg, log_msg)

test_class = Deprecated()
log_msg = log_warning.call_args[0][0]
self.assertIn('version=0.2.0', log_msg, log_msg)
self.assertIn('Class Deprecated', log_msg, log_msg)

call_arg = None

@deprecated("test deprecation", "1.0.0")
Expand Down