Skip to content

Commit f6b5329

Browse files
rabin-iomyakove
andauthored
Added a print_func_args parameter to control whether function arguments are included in log lines (#198)
Changes Made: 1. Added `print_func_args` parameter to TimeoutSampler.__init__(): • Default: True (backward compatible) • Controls whether function arguments and kwargs are included in log messages 2. Updated `_func_log` property: • Includes Args: and Kwargs: only when print_func_args=True • When False, logs only the function name and module 3. Updated `retry` decorator: • Added print_func_args parameter • Passes it through to TimeoutSampler Signed-off-by: Rabin Yasharzadehe <rabin@rabin.io> Co-authored-by: Meni Yakove <441263+myakove@users.noreply.github.com>
1 parent 4fd8a2d commit f6b5329

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

timeout_sampler/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class BExampleError(AExampleError)
8080
exceptions_dict (dict): Exception handling definition, only exception that specifically raised in exceptions_dict will be ignored
8181
print_log (bool): Print elapsed time to log.
8282
print_func_log (bool): Add function call info to log
83+
print_func_args (bool): Include function arguments in log when print_func_log is True
8384
"""
8485

8586
def __init__(
@@ -90,6 +91,7 @@ def __init__(
9091
exceptions_dict: dict[type[Exception], list[str]] | None = None,
9192
print_log: bool = True,
9293
print_func_log: bool = True,
94+
print_func_args: bool = True,
9395
func_args: tuple[Any] | None = None,
9496
**func_kwargs: Any,
9597
):
@@ -100,6 +102,7 @@ def __init__(
100102
self.func_kwargs = func_kwargs or {}
101103
self.print_log = print_log
102104
self.print_func_log = print_func_log
105+
self.print_func_args = print_func_args
103106
self.exceptions_dict = exceptions_dict if exceptions_dict is not None else {Exception: []}
104107

105108
def _get_func_info(self, _func: Callable, type_: str) -> Any:
@@ -122,8 +125,8 @@ def _get_func_info(self, _func: Callable, type_: str) -> Any:
122125

123126
@property
124127
def _func_log(self) -> str:
125-
_func_kwargs = f"Kwargs: {self.func_kwargs}" if self.func_kwargs else ""
126-
_func_args = f"Args: {self.func_args}" if self.func_args else ""
128+
_func_kwargs = f"Kwargs: {self.func_kwargs}" if (self.print_func_args and self.func_kwargs) else ""
129+
_func_args = f"Args: {self.func_args}" if (self.print_func_args and self.func_args) else ""
127130
_func_module = self._get_func_info(_func=self.func, type_="__module__")
128131
_func_name = self._get_func_info(_func=self.func, type_="__name__")
129132
return f"Function: {_func_module}.{_func_name} {_func_args} {_func_kwargs}".strip()
@@ -259,6 +262,7 @@ def retry(
259262
exceptions_dict: dict[type[Exception], list[str]] | None = None,
260263
print_log: bool = True,
261264
print_func_log: bool = True,
265+
print_func_args: bool = True,
262266
) -> Callable:
263267
"""
264268
Decorator for TimeoutSampler, For usage see TimeoutSampler.
@@ -280,6 +284,7 @@ def wrapper(*args: Any, **kwargs: dict[str, Any]) -> Any:
280284
exceptions_dict=exceptions_dict,
281285
print_log=print_log,
282286
print_func_log=print_func_log,
287+
print_func_args=print_func_args,
283288
func_args=args,
284289
**kwargs,
285290
):

0 commit comments

Comments
 (0)