there is another to implement decorator using class and you can also pass arguments to the decorator itself
here is an example of a logger helper decorator, which you can pass the function scope and the returned value when it fails
import logging
class LoggerHelper(object):
def __init__(self, scope, ret=False):
self.scope = scope
self.ret = ret
def __call__(self, original_function):
def inner_func(*args, **kwargs):
try:
logging.info(f"*** {self.scope} {original_function.__name__} Excuting ***")
return original_function(*args, **kwargs)
logging.info(f"*** {self.scope} {original_function.__name__} Executed Successfully ***")
except Exception as e:
logging.error(f"*** {self.scope} {original_function.__name__} Error: {str(e)} ***")
return self.ret
return inner_func
and when you use it, you can easily track where the exception was raised
class Example:
@LoggerHelper("Example", ret=False)
def method:
print(success)
return True