-
Notifications
You must be signed in to change notification settings - Fork 3
/
Logger.py
74 lines (49 loc) · 1.9 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
from datetime import datetime
import os
import sys
import platform
# global variable to store log file name for access from anywhere within application.
logger_file_name = ""
current_directory = ""
def get_current_time() -> str:
return datetime.now().strftime("%H:%M:%S.%f")
def get_datetime_string() -> str:
return datetime.now().strftime("%Y%m%d_%H%M%S")
def get_datetime_full_string() -> str:
return datetime.now().strftime("%d/%m/%Y %H:%M:%S")
def initalise_logging_session():
if platform.system() == 'Darwin':
path = __file__
else:
path = sys.argv[0]
global current_directory
current_directory = os.path.split(path)[0]
global logger_file_name
logger_file_name = f"peakmlviewerpy_log_{get_datetime_string()}.txt"
# Create log file
r = open(os.path.join(current_directory,logger_file_name), "w", encoding='utf-8')
r.write(f"PeakMLViewerPy Log - for session beginning {get_datetime_full_string()}")
r.write("\n")
r.write(f"=========================================================================")
r.close()
def log_error(details: str):
add_log_record_to_file(details, "ERROR")
def log_progress(details: str):
add_log_record_to_file(details, "PROGRESS")
def log_actions(details: str):
add_log_record_to_file(details, "ACTION")
def add_log_record_to_file(details: str, type: str):
global current_directory
# Skip if not currently in session (e.g. during automated testing)
if current_directory != "":
log_entry = f"{get_current_time()}: {type}: {details}"
#Open file in append mode
r = open(os.path.join(current_directory,logger_file_name), "a", encoding='utf-8')
# Add newline
r.write("\n")
# Add log entry
r.write(log_entry)
r.close()
def get_log():
f = open(os.path.join(current_directory,logger_file_name), "r", encoding='utf-8')
return f.read()