Last active
June 28, 2024 14:38
-
-
Save TJMac93/6a9e282f1d0aa8c46ce9b71155d5afb8 to your computer and use it in GitHub Desktop.
Logger to record the use of class methods. Use as decorator above method def and init class with verbose=True
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Logger gist available here: | |
# https://gist.github.com/TJMac93/cc17e0aa4546723cfba0d7165dbd9651 | |
from functools import wraps | |
from .logger import logger | |
import pandas as pd | |
def log_step(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
f_args = args[1:] | |
f_args = ["Pandas DF" if isinstance(arg, pd.DataFrame) else arg for arg in f_args] | |
if args[0].verbose: | |
# logger.info(f"Running function: {args[0].name}.{func.__name__}() with arguments: {f_args}") | |
pass | |
try: | |
result = func(*args, **kwargs) | |
except Exception as e: | |
logger.error(f"Failed to run function: {args[0].name}.{func.__name__}().\n\t\t\t\tException: {repr(e)}") | |
raise e | |
else: | |
if args[0].verbose: | |
logger.info(f"Successfully ran function: {args[0].name}.{func.__name__}().") | |
return result | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment