|
a |
|
b/logger.py |
|
|
1 |
import pandas as pd |
|
|
2 |
""" |
|
|
3 |
LOGGER |
|
|
4 |
""" |
|
|
5 |
LOG_QUEUE = [] |
|
|
6 |
LOGGING = True |
|
|
7 |
def log(msg=None,start=True,end_prev=True,new_level=False,end_level=False): |
|
|
8 |
if not LOGGING: return |
|
|
9 |
tag = '' |
|
|
10 |
now = pd.to_datetime('now') - pd.Timedelta(5, unit='h') |
|
|
11 |
if end_level: end_log_level() |
|
|
12 |
if end_prev and len(LOG_QUEUE) > 0: |
|
|
13 |
level = LOG_QUEUE.pop() |
|
|
14 |
if len(level) > 0: |
|
|
15 |
start_dt = level.pop() |
|
|
16 |
tag = '<<'*(len(LOG_QUEUE)) + ' --- ' |
|
|
17 |
__print_entry(now,tag,'({}s)'.format((now-start_dt).total_seconds())) |
|
|
18 |
LOG_QUEUE.append(level) |
|
|
19 |
if msg is None: return |
|
|
20 |
if start: |
|
|
21 |
if len(LOG_QUEUE) == 0: LOG_QUEUE.append([]) |
|
|
22 |
tag = '>>'*(len(LOG_QUEUE)-1) + ' ' |
|
|
23 |
LOG_QUEUE[-1].append(now) |
|
|
24 |
if new_level: LOG_QUEUE.append([]) |
|
|
25 |
else: tag = '' |
|
|
26 |
__print_entry(now,tag,msg) |
|
|
27 |
|
|
|
28 |
def __print_entry(dt,tag,msg): |
|
|
29 |
print '({}){}{}'.format(dt,tag,msg) |
|
|
30 |
|
|
|
31 |
|
|
|
32 |
def end_log_level(): |
|
|
33 |
if len(LOG_QUEUE) == 0: return |
|
|
34 |
while len(LOG_QUEUE[-1]) > 0 : log() |
|
|
35 |
LOG_QUEUE.pop() |
|
|
36 |
log() |
|
|
37 |
|
|
|
38 |
def end_log(): |
|
|
39 |
while len(LOG_QUEUE) > 0 : end_log_level() |
|
|
40 |
|
|
|
41 |
def stop_logging(): |
|
|
42 |
LOGGING = False |