Skip to content

Instantly share code, notes, and snippets.

@DoonDoony
Created April 23, 2019 05:50
Show Gist options
  • Save DoonDoony/462a3b541010455910a0f4dbd150d79c to your computer and use it in GitHub Desktop.
Save DoonDoony/462a3b541010455910a0f4dbd150d79c to your computer and use it in GitHub Desktop.
Simple Python Class Decorator
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