From 1a848afbeb8f6dc436e9cc987a5f74d1e18609c4 Mon Sep 17 00:00:00 2001 From: Vidit Ostwal Date: Mon, 27 Jan 2025 23:50:07 +0530 Subject: [PATCH] Added additional comments, refractored logging functionality --- src/crewai/crew.py | 2 +- src/crewai/utilities/file_handler.py | 71 ++++++++++++++++++---------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/crewai/crew.py b/src/crewai/crew.py index 07c4bfc0d..b84b4bf65 100644 --- a/src/crewai/crew.py +++ b/src/crewai/crew.py @@ -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) diff --git a/src/crewai/utilities/file_handler.py b/src/crewai/utilities/file_handler.py index 4600afa02..00ee59254 100644 --- a/src/crewai/utilities/file_handler.py +++ b/src/crewai/utilities/file_handler.py @@ -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)}") +