-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogcreator.py
57 lines (46 loc) · 1.69 KB
/
logcreator.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
'''
logcreator module of ChemAuto program.
Creating log file to monitor user input and program output
Developed by: Li Xilong
Last Update: 2024-04-01
'''
import os
import logging
from datetime import datetime, timedelta
def setup_logging():
# The same dir with .exe
log_file = 'chemauto.log'
if not os.path.exists(log_file):
open(log_file, 'w').close()
#Config log settings
logging.basicConfig(filename=log_file,
level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
def clean_logs():
log_file = 'chemauto.log'
retention_period = timedelta(days=3)
now = datetime.now()
if os.path.exists(log_file):
# Get the creation time of log file
log_file_time = os.path.getmtime(log_file)
# datetime.now() return a date but .getmtime() return a * seconds, transfer format before clac
log_creation_time = datetime.fromtimestamp(log_file_time)
if now - log_creation_time > retention_period:
try:
os.remove(log_file)
#print(f"Old log file {log_file} has been removed due to exceeding retention period.\n")
except Exception:
pass
def logged_input(prompt):
user_input = input(prompt)
#Record both prompt and user_input
logging.info(f"Input: {prompt}{user_input}")
return user_input
def logged_print(*args, **kwargs):
message = ' '.join(map(str, args))
logging.info(message)
print(*args, **kwargs)
if __name__ == "__main__":
clean_logs()
setup_logging()