Created
June 9, 2013 11:05
-
-
Save saurabh-hirani/5743174 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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