Skip to content

Instantly share code, notes, and snippets.

@saurabh-hirani
Created June 9, 2013 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurabh-hirani/5743174 to your computer and use it in GitHub Desktop.
Save saurabh-hirani/5743174 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import inspect
from functools import wraps
def get_line_number():
# inspect the current frame, go back 2 frames and get the line number.
return inspect.currentframe().f_back.f_back.f_lineno
def log_decorator_factory(loglevel):
def log_decorator_1(func):
@wraps(func)
def decorator_wrapper(*args):
print func.__name__ + " args - [" + ", ".join(args) + "]"
return decorator_wrapper
def log_decorator_2(func):
@wraps(func)
def decorator_wrapper(*args):
print func.__name__,
print " called_from_line:" + str(get_line_number()),
print " args - [" + ", ".join(args) + "]"
return decorator_wrapper
if (loglevel == 'info'):
return log_decorator_1
return log_decorator_2
@log_decorator_factory('info')
def func1(arg1, arg2):
print "entering func1"
@log_decorator_factory('debug')
def func2(arg1, arg2):
print "entering func2"
func1('this', 'that')
func2('who', 'what')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment