Enhance flow logging in EventListener and ConsoleFormatter by including flow ID in tree creation and status updates for better traceability.

This commit is contained in:
Lorenze Jay
2025-03-13 10:21:43 -07:00
parent 73801194d3
commit d946005ba2
2 changed files with 43 additions and 27 deletions

View File

@@ -176,14 +176,14 @@ class EventListener(BaseEventListener):
@crewai_event_bus.on(FlowCreatedEvent)
def on_flow_created(source, event: FlowCreatedEvent):
self._telemetry.flow_creation_span(event.flow_name)
self.formatter.create_flow_tree(event.flow_name)
self.formatter.create_flow_tree(event.flow_name, str(source.flow_id))
@crewai_event_bus.on(FlowStartedEvent)
def on_flow_started(source, event: FlowStartedEvent):
self._telemetry.flow_execution_span(
event.flow_name, list(source._methods.keys())
)
self.formatter.start_flow(event.flow_name)
self.formatter.start_flow(event.flow_name, str(source.flow_id))
@crewai_event_bus.on(FlowFinishedEvent)
def on_flow_finished(source, event: FlowFinishedEvent):

View File

@@ -121,8 +121,6 @@ class ConsoleFormatter:
ID=source_id,
)
self.print(tree)
self.print()
self.print_panel(content, title, style)
def create_crew_tree(self, crew_name: str, source_id: str) -> Optional[Tree]:
@@ -255,44 +253,42 @@ class ConsoleFormatter:
self.print(crew_tree)
self.print()
def create_flow_tree(self, flow_name: str) -> Tree:
def create_flow_tree(self, flow_name: str, flow_id: str) -> Optional[Tree]:
"""Create and initialize a flow tree."""
content = self.create_status_content(
"Starting Flow Execution", flow_name, "blue"
"Starting Flow Execution", flow_name, "blue", ID=flow_id
)
self.print_panel(content, "Flow Execution", "blue", is_flow=True)
# Create initial tree
# Create initial tree with flow ID
flow_label = Text()
flow_label.append("🌊 Flow: ", style="blue bold")
flow_label.append(flow_name, style="blue")
flow_label.append("\n ID: ", style="white")
flow_label.append(flow_id, style="blue")
flow_tree = Tree(flow_label)
self.add_tree_node(flow_tree, "✨ Created", "blue")
self.add_tree_node(flow_tree, "✅ Initialization Complete", "green")
self.print(flow_tree)
self.print()
# Set the current_flow_tree attribute directly
self.current_flow_tree = flow_tree
return flow_tree
def start_flow(self, flow_name: str) -> Optional[Tree]:
def start_flow(self, flow_name: str, flow_id: str) -> Optional[Tree]:
"""Initialize a flow execution tree."""
flow_tree = Tree("")
self.update_tree_label(flow_tree, "🌊 Flow:", flow_name, "blue", "In Progress")
self.add_tree_node(flow_tree, "🧠 Initializing...", "yellow")
flow_label = Text()
flow_label.append("🌊 Flow: ", style="blue bold")
flow_label.append(flow_name, style="blue")
flow_label.append("\n ID: ", style="white")
flow_label.append(flow_id, style="blue")
flow_tree.label = flow_label
self.add_tree_node(flow_tree, "🧠 Starting Flow...", "yellow")
self.print(flow_tree)
self.print()
# Set the current_flow_tree attribute directly
self.current_flow_tree = flow_tree
return flow_tree
def update_flow_status(
@@ -303,9 +299,10 @@ class ConsoleFormatter:
status: str = "completed",
) -> None:
"""Update flow status in the tree."""
if not self.verbose or flow_tree is None:
if flow_tree is None:
return
# Update main flow label
self.update_tree_label(
flow_tree,
"✅ Flow Finished:" if status == "completed" else "❌ Flow Failed:",
@@ -313,6 +310,19 @@ class ConsoleFormatter:
"green" if status == "completed" else "red",
)
# Update initialization node status
for child in flow_tree.children:
if "Starting Flow" in str(child.label):
child.label = Text(
(
"✅ Flow Completed"
if status == "completed"
else "❌ Flow Failed"
),
style="green" if status == "completed" else "red",
)
break
content = self.create_status_content(
(
"Flow Execution Completed"
@@ -323,6 +333,7 @@ class ConsoleFormatter:
"green" if status == "completed" else "red",
ID=flow_id,
)
self.print(flow_tree)
self.print_panel(
content, "Flow Completion", "green" if status == "completed" else "red"
)
@@ -335,16 +346,25 @@ class ConsoleFormatter:
status: str = "running",
) -> Optional[Tree]:
"""Update method status in the flow tree."""
# Early return if verbose is disabled or flow_tree is None
if not self.verbose or flow_tree is None:
if not flow_tree:
return None
if status == "running":
prefix, style = "🔄 Running:", "yellow"
elif status == "completed":
prefix, style = "✅ Completed:", "green"
# Update initialization node when a method completes successfully
for child in flow_tree.children:
if "Starting Flow" in str(child.label):
child.label = Text("Flow Method Step", style="white")
break
else:
prefix, style = "❌ Failed:", "red"
# Update initialization node on failure
for child in flow_tree.children:
if "Starting Flow" in str(child.label):
child.label = Text("❌ Flow Step Failed", style="red")
break
if not method_branch:
# Find or create method branch
@@ -361,10 +381,6 @@ class ConsoleFormatter:
self.print(flow_tree)
self.print()
# Set the current_method_branch attribute directly
self.current_method_branch = method_branch
return method_branch
def handle_tool_usage_started(