-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.py
115 lines (84 loc) · 3.53 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
from datetime import datetime as dt
import sys
import os
import json
_HTML_START = "<HEAD><meta http-equiv='refresh' content='100' ></HEAD><BODY><pre>"
_HTML_END = "</pre></BODY>"
class Logger():
def __init__(self, logs_folder = "logs", models_folder = "models",
output_folder = "output", data_folder = "data",
show = False, verbosity_level = 0, html_output = False,
config_file = "config.txt"):
sys.stdout.flush()
print(self.get_time() + " Initialize the logger")
self.internal_clock = dt.now()
self.logs_folder = logs_folder
self.models_folder = models_folder
self.output_folder = output_folder
self.data_folder = data_folder
self.show = show
self.verbosity_level = verbosity_level
self.html_output = html_output
self.config_file = config_file
if not os.path.exists(logs_folder):
os.makedirs(logs_folder)
print(self.get_time() + " Create logs folder {}".format(logs_folder))
self.log_file = self.create_file()
if self.html_output:
self.log_file.write(_HTML_START)
self.log_file.close()
if not os.path.exists(models_folder):
os.makedirs(models_folder)
print(self.get_time() + " Create models folder {}".format(models_folder))
if not os.path.exists(output_folder):
os.makedirs(output_folder)
print(self.get_time() + " Create output folder {}".format(output_folder))
if not os.path.exists(data_folder):
os.makedirs(data_folder)
print(self.get_time() + " Create data folder {}".format(data_folder))
with open(self.config_file) as fp:
self.config_dict = json.load(fp)
print(self.get_time() + " Read config file {}".format(config_file))
def get_time(self):
return dt.strftime(dt.now(), '%Y.%m.%d-%H:%M:%S')
def get_model_file(self, filename, additional_path_to_file = ""):
return os.path.join(self.models_folder, additional_path_to_file, filename)
def get_output_file(self, filename, additional_path_to_file = ""):
return os.path.join(self.output_folder, additional_path_to_file, filename)
def get_data_file(self, filename, additional_path_to_file = ""):
return os.path.join(self.data_folder, additional_path_to_file, filename)
def get_time_prefix(self):
return dt.strftime(dt.now(), '%Y-%m-%d_%H_%M_%S')
def create_file(self):
time_prefix = dt.strftime(dt.now(), '%Y-%m-%d_%H_%M_%S')
for i in range(sys.maxsize):
log_path = os.path.join(self.logs_folder, time_prefix + "_" + "log" + str(i))
if self.html_output:
log_path += ".html"
else:
log_path += ".txt"
if not os.path.exists(log_path):
print(self.get_time() + " Create log file {}".format(log_path))
self.log_filename = log_path
return open(log_path, 'w')
def change_show(self, show):
self.show = show
def close(self):
if self.html_output:
with open(self.log_filename, "a") as fp:
fp.write(_HTML_END)
def log(self, str_to_log, show = None, tabs = 0, verbosity_level = 0, show_time = False):
sys.stdout.flush()
if show_time:
str_to_log += " [{:.2f}s]".format((dt.now() - self.internal_clock).total_seconds())
self.internal_clock = dt.now()
if show is None:
show = self.show
if verbosity_level < self.verbosity_level:
show = False
time_prefix = dt.strftime(dt.now(), '[%Y.%m.%d-%H:%M:%S] ')
if show:
print(time_prefix + tabs * '\t' + str_to_log, flush=True)
with open(self.log_filename, "a") as fp:
fp.write(time_prefix + str_to_log)
fp.write("\n")