-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
148 lines (112 loc) · 3.87 KB
/
logger.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import time
import logging
import collections
import PyUtilities.common
debugging = False
logger = logging.getLogger(__name__)
def setLogger(label=None):
global logger
logger = logging.getLogger(label)
return logger
def logger_debug(silence_azure=True, silence_urlib=False, silence_paramiko=True):
""" Changes the log level to debug.
See: https://docs.python.org/3/howto/logging.html
Example Input: logger_debug()
Example Input: logger_debug(silence_azure=False)
"""
global debugging
debugging = True # Use this to short-circuit expensive f-strings
logging.basicConfig(level=logging.DEBUG)
if (silence_azure):
logging.getLogger('azure.core.pipeline.policies.http_logging_policy').setLevel(logging.WARNING)
if (silence_urlib):
logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
if (silence_paramiko):
logging.getLogger('paramiko.transport').setLevel(logging.WARNING)
def logger_info(silence_azure=True, silence_urlib=False, silence_paramiko=True):
""" Changes the log level to info.
See: https://docs.python.org/3/howto/logging.html
Example Input: logger_info()
Example Input: logger_info(silence_azure=False)
"""
logging.basicConfig(level=logging.INFO)
if (silence_azure):
logging.getLogger('azure.core.pipeline.policies.http_logging_policy').setLevel(logging.WARNING)
if (silence_urlib):
logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
if (silence_paramiko):
logging.getLogger('paramiko.transport').setLevel(logging.WARNING)
logger_timers = collections.defaultdict(dict)
def logger_timer(label=None):
""" A decorator that records how long a function took to execute.
If the logger level is not info or debug, will not do anything.
See: https://docs.python.org/3/howto/logging.html#optimization
label (str) - What the timer is called
- If None: Will assign a unique number as the label for this timer
EXAMPLE USE
@logger_timer()
def longFunction():
pass
EXAMPLE USE
@logger_timer("lorem")
def longFunction():
pass
"""
if (not logger.isEnabledFor(logging.INFO)):
def decorator(myFunction):
return myFunction
return decorator
def decorator(myFunction):
def wrapper(*args, **kwargs):
nonlocal label
label = logger_timer_start(label)
answer = myFunction(*args, **kwargs)
logger_timer_end(label)
return answer
return wrapper
return decorator
def logger_timer_start(label=None):
if (label is None):
label = len(logger_timers)
while label in logger_timers:
label += 1
catalogue = logger_timers[label]
# logging.info(f"Starting the '{label}' timer")
catalogue["start"] = time.perf_counter()
return label
def logger_timer_end(label):
# logging.info(f"Ending the '{label}' timer")
catalogue = logger_timers[label]
catalogue["end"] = time.perf_counter()
def logger_timer_print(label=None):
""" Prints the times for timers.
label (str) - Which timer to print the time for
- If None: Will print for all timers
- If tuple: WIll print for each timer in the list
Example Input: logger_timer_print()
Example Input: logger_timer_print("lorem")
"""
for _label in PyUtilities.common.ensure_container(label, convertNone=False, returnForNone=lambda: logger_timers.keys()):
catalogue = logger_timers.get(_label)
start_time = catalogue.get("start")
if (start_time is not None):
logging.info(f"{_label}: {catalogue.get('end', time.perf_counter()) - start_time:.2f}")
def logger_runtime(log_open, log_save):
""" A decorator that starts directing stdout and stderr to a handler for the duration of a function
EXAMPLE USE
@logger_runtime()
def longFunction(log_open=log_open, log_save=log_save):
pass
"""
def decorator(myFunction):
def wrapper(self, *args, **kwargs):
log_open()
try:
answer = myFunction(self, *args, **kwargs)
except Exception as error:
log_save(error=error)
raise error
log_save()
return answer
return wrapper
return decorator