Skip to content

Instantly share code, notes, and snippets.

@blaxter
Last active December 14, 2015 04:48
Show Gist options
  • Save blaxter/5030477 to your computer and use it in GitHub Desktop.
Save blaxter/5030477 to your computer and use it in GitHub Desktop.
log method decorator
import logging
def log_method(fun):
"""
Example output (it will depend of you logging setup, obviously):
2013-02-25 16:13:58+0100 [-] DEBUG:root:--> Foo.update_bar
2013-02-25 16:13:58+0100 [-] DEBUG:root:<-- Foo.update_bar (0.052)
"""
def with_logging(self, *args, **kwargs):
method_name = '%s.%s' % (self.__class__.__name__, fun.__name__)
logging.debug('--> %s' % method_name)
start = time.time()
fun(self, *args, **kwargs)
logging.debug('<-- %s (%.3f)' % (method_name, time.time() - start))
return with_logging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment