Skip to content

Instantly share code, notes, and snippets.

@rizsotto
Last active August 29, 2015 13:59
Show Gist options
  • Save rizsotto/10884019 to your computer and use it in GitHub Desktop.
Save rizsotto/10884019 to your computer and use it in GitHub Desktop.
TRACE decorator with indent. using standard python logging.
import logging
import functools
import traceback
def trace(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
indent = ' ' * len(traceback.extract_stack())
try:
logging.debug('{0}entering {1}'.format(indent, fn.__name__))
return fn(*args, **kwargs)
except:
logging.debug('{0}exception in {1}'.format(indent, fn.__name__))
raise
else:
logging.debug('{0}leaving {1}'.format(indent, fn.__name__))
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment