mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-07 10:12:38 +00:00
Added functionality to have json format as well for the logs
This commit is contained in:
@@ -185,6 +185,10 @@ class Crew(BaseModel):
|
|||||||
output_log_file: Optional[str] = Field(
|
output_log_file: Optional[str] = Field(
|
||||||
default=None,
|
default=None,
|
||||||
description="output_log_file",
|
description="output_log_file",
|
||||||
|
),
|
||||||
|
save_as_json: Optional[bool] = Field(
|
||||||
|
default=False,
|
||||||
|
description="If true saves the logs in JSON format",
|
||||||
)
|
)
|
||||||
planning: Optional[bool] = Field(
|
planning: Optional[bool] = Field(
|
||||||
default=False,
|
default=False,
|
||||||
@@ -244,7 +248,7 @@ class Crew(BaseModel):
|
|||||||
self._cache_handler = CacheHandler()
|
self._cache_handler = CacheHandler()
|
||||||
self._logger = Logger(verbose=self.verbose)
|
self._logger = Logger(verbose=self.verbose)
|
||||||
if self.output_log_file:
|
if self.output_log_file:
|
||||||
self._file_handler = FileHandler(self.output_log_file)
|
self._file_handler = FileHandler(self.output_log_file,self.save_as_json)
|
||||||
self._rpm_controller = RPMController(max_rpm=self.max_rpm, logger=self._logger)
|
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):
|
if self.function_calling_llm and not isinstance(self.function_calling_llm, LLM):
|
||||||
self.function_calling_llm = create_llm(self.function_calling_llm)
|
self.function_calling_llm = create_llm(self.function_calling_llm)
|
||||||
|
|||||||
@@ -5,24 +5,48 @@ from datetime import datetime
|
|||||||
|
|
||||||
class FileHandler:
|
class FileHandler:
|
||||||
"""take care of file operations, currently it only logs messages to a file"""
|
"""take care of file operations, currently it only logs messages to a file"""
|
||||||
|
|
||||||
def __init__(self, file_path):
|
def __init__(self, file_path, save_as_json):
|
||||||
if isinstance(file_path, bool):
|
self.save_as_json = save_as_json
|
||||||
self._path = os.path.join(os.curdir, "logs.txt")
|
if file_path is True: # File path is boolean True
|
||||||
elif isinstance(file_path, str):
|
if save_as_json:
|
||||||
|
self._path = os.path.join(os.curdir, "logs.json")
|
||||||
|
else:
|
||||||
|
self._path = os.path.join(os.curdir, "logs.txt")
|
||||||
|
elif isinstance(file_path, str): # File path is a string
|
||||||
|
if save_as_json:
|
||||||
|
if not file_path.endswith(".json"):
|
||||||
|
file_path += ".json"
|
||||||
self._path = file_path
|
self._path = file_path
|
||||||
else:
|
else:
|
||||||
raise ValueError("file_path must be either a boolean or a string.")
|
raise ValueError("file_path must be either a boolean or a string.")
|
||||||
|
|
||||||
def log(self, **kwargs):
|
def log(self, **kwargs):
|
||||||
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
message = (
|
log_entry = {"timestamp": now, **kwargs}
|
||||||
f"{now}: "
|
|
||||||
+ ", ".join([f'{key}="{value}"' for key, value in kwargs.items()])
|
if self._path.endswith(".json"):
|
||||||
+ "\n"
|
# Append log in JSON format
|
||||||
)
|
with open(self._path, "a", encoding="utf-8") as file:
|
||||||
with open(self._path, "a", encoding="utf-8") as file:
|
# If the file is empty, start with a list; else, append to it
|
||||||
file.write(message + "\n")
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PickleHandler:
|
class PickleHandler:
|
||||||
|
|||||||
Reference in New Issue
Block a user