forked from smilejay/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decorator.py
More file actions
31 lines (22 loc) · 680 Bytes
/
test_decorator.py
File metadata and controls
31 lines (22 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from functools import wraps
''' useful doc: https://www.runoob.com/w3cnote/python-func-decorators.html '''
def logit(logfile='logit.log'):
def logging_decorator(func):
@wraps(func)
def wrapped_function(*args, **kwargs):
log_string = func.__name__ + " was called"
print(log_string)
with open(logfile, 'a') as opened_file:
opened_file.write(log_string + '\n')
return func(*args, **kwargs)
return wrapped_function
return logging_decorator
@logit()
def myfunc1():
pass
@logit(logfile='func2.log')
def myfunc2():
pass
if __name__ == '__main__':
myfunc1()
myfunc2()