-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.py
More file actions
31 lines (26 loc) · 849 Bytes
/
logging.py
File metadata and controls
31 lines (26 loc) · 849 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
29
30
31
"""Logging system for training runs."""
import pickle
import os
class FileLogger:
def __init__(self, filepath):
self.text_path = filepath
self.pickle_path = filepath + ".pkl"
def log(self, name, entry):
# Log as readable text
with open(self.text_path, "a") as f:
f.write(f"{name}: {entry}\n")
# Append to pickle log
with open(self.pickle_path, "ab") as pf:
pickle.dump((name, entry), pf)
def load_logs(self):
"""Load all logs from the pickle file."""
if not os.path.exists(self.pickle_path):
return []
logs = []
with open(self.pickle_path, "rb") as pf:
try:
while True:
logs.append(pickle.load(pf))
except EOFError:
pass
return logs