Added additional comments, refractored logging functionality

This commit is contained in:
Vidit Ostwal
2025-01-27 23:50:07 +05:30
parent 5e4a815b0e
commit 1a848afbeb
2 changed files with 46 additions and 27 deletions

View File

@@ -248,7 +248,7 @@ class Crew(BaseModel):
self._cache_handler = CacheHandler()
self._logger = Logger(verbose=self.verbose)
if self.output_log_file:
self._file_handler = FileHandler(self.output_log_file,self.save_as_json)
self._file_handler = FileHandler(self.output_log_file, self.save_as_json)
self._rpm_controller = RPMController(max_rpm=self.max_rpm, logger=self._logger)
if self.function_calling_llm and not isinstance(self.function_calling_llm, LLM):
self.function_calling_llm = create_llm(self.function_calling_llm)

View File

@@ -1,13 +1,24 @@
import os
import pickle
from datetime import datetime
from typing import Union
import json
class FileHandler:
"""take care of file operations, currently it only logs messages to a file"""
"""Handler for file operations supporting both JSON and text-based logging.
def __init__(self, file_path, save_as_json):
Args:
file_path (Union[bool, str]): Path to the log file or boolean flag
save_as_json (bool): If True, saves logs in JSON format
"""
def __init__(self, file_path: Union[bool, str], save_as_json: bool):
self.save_as_json = save_as_json
self._path = self._initialize_path(file_path, save_as_json)
def _initialize_path(self, file_path: Union[bool, str], save_as_json: bool) -> str:
if file_path is True: # File path is boolean True
if save_as_json:
self._path = os.path.join(os.curdir, "logs.json")
@@ -21,31 +32,39 @@ class FileHandler:
else:
raise ValueError("file_path must be either a boolean or a string.")
def log(self, **kwargs):
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_entry = {"timestamp": now, **kwargs}
if self._path.endswith(".json"):
# Append log in JSON format
with open(self._path, "a", encoding="utf-8") as file:
# If the file is empty, start with a list; else, append to it
try:
# Try reading existing content to avoid overwriting
with open(self._path, "r", encoding="utf-8") as read_file:
existing_data = json.load(read_file)
existing_data.append(log_entry)
except (json.JSONDecodeError, FileNotFoundError):
# If no valid JSON or file doesn't exist, start with an empty list
existing_data = [log_entry]
with open(self._path, "w", encoding="utf-8") as write_file:
json.dump(existing_data, write_file, indent=4)
write_file.write("\n")
else:
# Append log in plain text format
message = f"{now}: " + ", ".join([f"{key}=\"{value}\"" for key, value in kwargs.items()]) + "\n"
with open(self._path, "a", encoding="utf-8") as file:
file.write(message)
def log(self, **kwargs):
try:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_entry = {"timestamp": now, **kwargs}
if self._path.endswith(".json"):
# Append log in JSON format
with open(self._path, "a", encoding="utf-8") as file:
# If the file is empty, start with a list; else, append to it
try:
# Try reading existing content to avoid overwriting
with open(self._path, "r", encoding="utf-8") as read_file:
existing_data = json.load(read_file)
existing_data.append(log_entry)
except (json.JSONDecodeError, FileNotFoundError):
# If no valid JSON or file doesn't exist, start with an empty list
existing_data = [log_entry]
with open(self._path, "w", encoding="utf-8") as write_file:
json.dump(existing_data, write_file, indent=4)
write_file.write("\n")
else:
# Append log in plain text format
message = f"{now}: " + ", ".join([f"{key}=\"{value}\"" for key, value in kwargs.items()]) + "\n"
with open(self._path, "a", encoding="utf-8") as file:
file.write(message)
except Exception as e:
raise ValueError(f"Failed to log message: {str(e)}")