Created
April 23, 2019 05:50
-
-
Save DoonDoony/462a3b541010455910a0f4dbd150d79c to your computer and use it in GitHub Desktop.
Simple Python Class Decorator
This file contains hidden or 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
import sys | |
import logging | |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) | |
class logger: | |
def __init__(self, function): | |
self.function = function | |
self.logger = logging.getLogger(__name__) | |
def __call__(self, *args, **kwargs): | |
self.logger.info('{} is executing'.format(self.function.__name__)) | |
return self.function(*args, **kwargs) | |
@logger | |
def add(a, b): | |
return a + b | |
if __name__ == '__main__': | |
add(1, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment