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

chore: Allow logging without inheritance #2257

Merged
merged 16 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 35 additions & 0 deletions core/src/main/python/synapse/ml/core/logging/SynapseMLLogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,41 @@ def log_decorator_wrapper(self, *args, **kwargs):

return get_wrapper

@staticmethod
def log_verb_static(method_name: Optional[str] = None):
def get_wrapper(func):
@functools.wraps(func)
def log_decorator_wrapper(self, *args, **kwargs):
if not hasattr(self, 'logger'):
raise AttributeError(f"{self.__class__.__name__} does not have a 'logger' attribute. "
"Ensure a logger instance is initialized in the constructor.")
logger = self.logger
start_time = time.perf_counter()
try:
result = func(self, *args, **kwargs)
execution_time = logger._round_significant(
time.perf_counter() - start_time, 3
)
logger._log_base(
func.__module__,
method_name if method_name else func.__name__,
logger.get_column_number(args, kwargs),
execution_time,
None,
)
return result
except Exception as e:
logger._log_error_base(
func.__module__,
method_name if method_name else func.__name__,
e,
)
raise

return log_decorator_wrapper

return get_wrapper

def log_class(self, feature_name: str):
return self._log_base("constructor", None, None, None, feature_name)

Expand Down
28 changes: 28 additions & 0 deletions core/src/test/python/synapsemltest/core/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ def fit(self, df):
def test_throw(self):
raise Exception("test exception")

class NoInheritTransformer():
def __init__(self):
self.logger = SynapseMLLogger(log_level=logging.DEBUG)

@SynapseMLLogger.log_verb_static(method_name="transform")
def transform(self, df):
return True

@SynapseMLLogger.log_verb_static(method_name="fit")
def fit(self, df):
return True

@SynapseMLLogger.log_verb_static()
def test_throw(self):
raise Exception("test exception")


class LoggingTest(unittest.TestCase):
def test_logging_smoke(self):
Expand All @@ -44,6 +60,18 @@ def test_logging_smoke(self):
except Exception as e:
assert f"{e}" == "test exception"

def test_logging_smoke_no_inheritance(self):
t = NoInheritTransformer()
data = [("Alice", 25), ("Bob", 30), ("Charlie", 35)]
columns = ["name", "age"]
df = sc.createDataFrame(data, columns)
t.transform(df)
t.fit(df)
try:
t.test_throw()
except Exception as e:
assert f"{e}" == "test exception"


if __name__ == "__main__":
result = unittest.main()
Loading