forked from M507/RamiGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_logger.py
More file actions
79 lines (66 loc) · 3.22 KB
/
Copy pathsetup_logger.py
File metadata and controls
79 lines (66 loc) · 3.22 KB
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
import logging
from datetime import datetime, timedelta
import os
# Configure loggers
def setup_logger(name, file, level, format):
logger = logging.getLogger(name)
logger.setLevel(level)
handler = logging.FileHandler(file)
handler.setLevel(level)
formatter = logging.Formatter(format)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
time_logger = setup_logger('TimeLogger', 'times.log', logging.INFO, '%(asctime)s - %(message)s')
debug_logger = setup_logger('DebugLogger', 'debug.log', logging.DEBUG, '%(asctime)s %(levelname)s:%(message)s')
# Markdown table manager class
class MarkdownTableLogger:
table_file = "README.md"
@staticmethod
def append_to_table(custom_message, start_time, end_time, elapsed_time):
row = f"| {custom_message} | Link | {start_time} | {end_time} | {elapsed_time} |\n"
if not os.path.exists(MarkdownTableLogger.table_file):
content = "# RamiGPT\n\nThis is a tool.\n\n## Timing Table\n\n| Task Description | Start Time | End Time | Elapsed Time |\n|------------------|------------|----------|--------------|\n"
with open(MarkdownTableLogger.table_file, 'w') as file:
file.write(content + row)
return
with open(MarkdownTableLogger.table_file, 'r+') as file:
content = file.readlines()
timing_table_index = -1
for i, line in enumerate(content):
if "## Timing Table" in line:
timing_table_index = i + 2 # Finding headers
while timing_table_index < len(content) and "|" in content[timing_table_index]:
timing_table_index += 1
break
if timing_table_index == -1:
# Timing table section not found, create it at the end of the file
content.append("\n## Timing Table\n")
content.append("| Task Description | Link | Start Time | End Time | Elapsed Time |\n")
content.append("|------------------|------------|------------|----------|--------------|\n")
content.append(row)
else:
# Correctly insert the new row under the last row of the table
content.insert(timing_table_index, row)
file.seek(0)
file.writelines(content)
file.truncate()
# Global timer class
class GlobalTimer:
start_time = None
@staticmethod
def start():
GlobalTimer.start_time = datetime.now()
debug_logger.debug("Timer started.")
@staticmethod
def stop(custom_message):
if GlobalTimer.start_time is None:
debug_logger.error("Timer has not been started.")
return
end_time = datetime.now()
elapsed_time = end_time - GlobalTimer.start_time
time_logger.info(f"{custom_message} - Start Time: {GlobalTimer.start_time}, End Time: {end_time}, Elapsed Time: {elapsed_time}")
debug_logger.debug(f"Timer stopped. {custom_message} - Elapsed Time: {elapsed_time}")
# Log to Markdown table
MarkdownTableLogger.append_to_table(custom_message, GlobalTimer.start_time, end_time, elapsed_time)
GlobalTimer.start_time = None