-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.py
54 lines (36 loc) · 1.44 KB
/
decorator.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
import logging
from functools import wraps
def logThis(filename=None, timed=True):
"""Decorator logging the method called, the arguments it was called with
as well as the time it took it to execute in log file.
Args:
filename (string): Name of the log file.
timed (bool, optional): If True measures execution time and logs it.
Returns:
func: The wrapped function.
"""
def decorate(function):
logname = filename if filename else 'logfile.log'
log = logging.getLogger(logname)
logging.basicConfig(filename=logname, level=logging.INFO)
if timed is True:
@wraps(function)
def wrapper(*args, **kwargs):
start = time.time()
result = function(*args, **kwargs)
end = time.time()
duration = end - start
log.log(logging.INFO, '{0} ran in {1:.4f} seconds with args: {2}, and kwargs: {3}'.format(
function.__name__, duration, args, kwargs))
return result
else:
@wraps(function)
def wrapper(*args, **kwargs):
result = function(*args, **kwargs)
log.log(logging.INFO,
'{0} ran with args: {1}, and kwargs: {2}'
.format(function.__name__, args, kwargs))
return result
return wrapper
return decorate