forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
28 lines (22 loc) · 726 Bytes
/
Copy pathlogger.py
File metadata and controls
28 lines (22 loc) · 726 Bytes
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
import csv
import time
import threading
class MetricsLogger:
def __init__(self):
self.logs = []
self._lock = threading.Lock()
def log(self, metrics: dict):
if not isinstance(metrics, dict):
raise TypeError("metrics must be a dictionary")
with self._lock:
entry = {'timestamp': time.time()}
entry.update(metrics)
self.logs.append(entry)
def export_csv(self, path):
if not self.logs:
return
keys = self.logs[0].keys()
with open(path, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=keys)
writer.writeheader()
writer.writerows(self.logs)