Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/art/langgraph/logging.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import json
import os
import pickle


class FileLogger:
def __init__(self, filepath):
self.text_path = filepath
self.pickle_path = filepath + ".pkl"
self.jsonl_path = filepath + ".jsonl"

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)
# Append to jsonl log
with open(self.jsonl_path, "a") as jf:
jf.write(json.dumps([name, entry]) + "\n")

def load_logs(self):
"""Load all logs from the pickle file."""
if not os.path.exists(self.pickle_path):
"""Load all logs from the jsonl file."""
if not os.path.exists(self.jsonl_path):
return []
logs = []
with open(self.pickle_path, "rb") as pf:
try:
while True:
logs.append(pickle.load(pf))
except EOFError:
pass
with open(self.jsonl_path, "r") as jf:
for line in jf:
line = line.strip()
if line:
logs.append(tuple(json.loads(line)))
return logs