-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathatexit_session_logger_github.py
More file actions
46 lines (35 loc) · 1.36 KB
/
atexit_session_logger_github.py
File metadata and controls
46 lines (35 loc) · 1.36 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
import atexit
from datetime import datetime
import tempfile
import os
class SessionLogger:
"""Automatically logs when a session starts and ends using atexit."""
def __init__(self):
self.temp_dir = tempfile.gettempdir()
self.log_file = os.path.join(self.temp_dir, "session_log.txt")
self._log_start()
# Register cleanup automatically
atexit.register(self._log_end)
def _log_start(self):
with open(self.log_file, "a", encoding="utf-8") as f:
f.write(f"Session started at {datetime.now()}\n")
print(f"Session started. Log file: {self.log_file}")
def _log_end(self):
with open(self.log_file, "a", encoding="utf-8") as f:
f.write(f"Session ended at {datetime.now()}\n\n")
print("! Session end logged automatically.")
def run_task(self):
print("Running some important work...")
for i in range(3):
print(f" - Step {i+1} completed")
def run_task_again(self):
print("Running some important work again...")
for i in range(3):
print(f" - Step {i+1} completed")
def main():
logger = SessionLogger()
logger.run_task()
logger.run_task_again()
print("Program will now exit — cleanup handled automatically!")
if __name__ == "__main__":
main()