-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.py
76 lines (61 loc) · 1.98 KB
/
logs.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
import re
import sys
import json
import uuid
import os
from datetime import datetime
class Logs():
def __init__(self):
self.logs = []
self.warnings = []
self.runtime_started = self.get_datetime_str()
self.runtime_id = str(uuid.uuid4())
self.old_out = sys.stdout
"""Stamped stdout."""
self.nl = True
def get_datetime_str(self):
return str(datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'))
def write(self, log):
"""Write function overloaded."""
if log == '\n':
self.old_out.write(log)
self.nl = True
elif self.nl:
log_withdatetime = '%s:%s> %s' % (self.runtime_id, self.get_datetime_str(), log)
self.old_out.write(log_withdatetime)
self.logs.append(log_withdatetime)
self.nl = False
else:
self.old_out.write(log)
self.logs.append(log)
if re.match("^(Warning.*)$", log):
self.warnings.append(log_withdatetime)
if re.match("^(Error.*)$", log):
self.do_exit()
def flush(self):
pass
def send_logs(self):
print('Sending logs...')
logs_folder = './logs/'
logs_json = json.dumps(
{
"id": self.runtime_id,
"start": self.runtime_started,
"warnings": self.warnings,
"logs": self.logs
}
)
logs_filename = '{0}_{1}.json'.format(datetime.strftime(datetime.now(), '%Y%m-%d-%H%M%S'), self.runtime_id)
if not os.path.exists(logs_folder):
os.makedirs(logs_folder)
try:
with open(logs_folder+'/'+logs_filename, 'w') as logs_file:
logs_file.write(logs_json)
except:
print('Error: Impossible to write logs')
def do_exit(self):
print('Exiting...')
sys.exit()
def end_process(self):
print('Stopping...')
self.send_logs()