Skip to content

Commit 05daca5

Browse files
Merge pull request #266 from katzlabbrandeis/20-add-logging-for-all-scripts
20 add logging for all scripts
2 parents 53182d3 + 434fb2e commit 05daca5

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

blech_clust.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ def initialize_groups(self):
102102
found_list.append(this_group)
103103

104104
if len(found_list) > 0 and not self.force_run:
105-
print(f'Data already present: {found_list}')
105+
reload_msg = f'Data already present: {found_list}' + '\n' +\
106+
'Reload data? (yes/y/n/no) ::: '
106107
reload_data_str, continue_bool = entry_checker(
107-
msg='Reload data? (yes/y/n/no) ::: ',
108+
msg= reload_msg,
108109
check_func=lambda x: x in ['y', 'yes', 'n', 'no'],
109110
fail_response='Please enter (yes/y/n/no)')
110111
else:

utils/blech_utils.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,32 @@
66
import glob
77
import json
88
import pandas as pd
9+
import sys
910
from datetime import datetime
1011

12+
class Tee:
13+
"""Tee output to both stdout/stderr and a log file"""
14+
def __init__(self, data_dir, name='output.log'):
15+
self.log_path = os.path.join(data_dir, name)
16+
self.file = open(self.log_path, 'a')
17+
self.stdout = sys.stdout
18+
self.stderr = sys.stderr
19+
sys.stdout = self
20+
sys.stderr = self
21+
22+
def write(self, data):
23+
self.file.write(data)
24+
self.stdout.write(data)
25+
self.file.flush()
26+
27+
def flush(self):
28+
self.file.flush()
29+
30+
def close(self):
31+
sys.stdout = self.stdout
32+
sys.stderr = self.stderr
33+
self.file.close()
34+
1135
class path_handler():
1236

1337
def __init__(self):
@@ -28,9 +52,10 @@ class pipeline_graph_check():
2852
"""
2953

3054
def __init__(self, data_dir):
55+
self.data_dir = data_dir
56+
self.tee = Tee(data_dir)
3157
self.load_graph()
3258
self.check_graph()
33-
self.data_dir = data_dir
3459

3560
def load_graph(self):
3661
"""
@@ -129,10 +154,19 @@ def write_to_log(self, script_path, type = 'attempted'):
129154
if 'attempted' not in log_dict.keys():
130155
log_dict['attempted'] = {}
131156
log_dict['attempted'][script_path] = current_datetime
157+
print('============================================================')
158+
print(f'Attempting {os.path.basename(script_path)}, started at {current_datetime}')
159+
print('============================================================')
132160
elif type == 'completed':
133161
if 'completed' not in log_dict.keys():
134162
log_dict['completed'] = {}
135163
log_dict['completed'][script_path] = current_datetime
164+
print('============================================================')
165+
print(f'Completed {os.path.basename(script_path)}, ended at {current_datetime}')
166+
print('============================================================')
167+
# Close tee when completing
168+
if hasattr(self, 'tee'):
169+
self.tee.close()
136170
# log_dict[script_path] = current_datetime
137171
with open(self.log_path, 'w') as log_file_connect:
138172
json.dump(log_dict, log_file_connect, indent = 4)

0 commit comments

Comments
 (0)