Workflow logger that tracks the entire execution lifecycle.
| 63 | |
| 64 | |
| 65 | class WorkflowLogger: |
| 66 | """Workflow logger that tracks the entire execution lifecycle.""" |
| 67 | |
| 68 | def __init__(self, workflow_id: str = None, log_level: LogLevel = LogLevel.DEBUG, use_structured_logging: bool = True, log_to_console: bool = True): |
| 69 | self.workflow_id = workflow_id or f"workflow_{datetime.now().strftime('%Y%m%d_%H%M%S')}" |
| 70 | self.logs: List[LogEntry] = [] |
| 71 | self.start_time = datetime.now() |
| 72 | self.current_path: List[str] = [] |
| 73 | self.log_level: LogLevel = log_level |
| 74 | |
| 75 | self.log_to_console: bool = log_to_console |
| 76 | self.use_structured_logging = use_structured_logging |
| 77 | self.structured_logger: Optional[StructuredLogger] = None |
| 78 | if use_structured_logging: |
| 79 | self.structured_logger = get_workflow_logger(self.workflow_id) |
| 80 | |
| 81 | def add_log(self, level: LogLevel, message: str = None, node_id: str = None, |
| 82 | event_type: EventType = None, details: Dict[str, Any] = None, |
| 83 | duration: float = None) -> LogEntry | None: |
| 84 | """Add a log entry.""" |
| 85 | if level < self.log_level: |
| 86 | return None |
| 87 | |
| 88 | timestamp = datetime.now().isoformat() |
| 89 | execution_path = copy.deepcopy(self.current_path) |
| 90 | |
| 91 | safe_details = _json_safe(details or {}) |
| 92 | |
| 93 | log_entry = LogEntry( |
| 94 | timestamp=timestamp, |
| 95 | level=level, |
| 96 | node_id=node_id, |
| 97 | event_type=event_type, |
| 98 | message=message, |
| 99 | details=safe_details, |
| 100 | execution_path=execution_path, |
| 101 | duration=duration |
| 102 | ) |
| 103 | self.logs.append(log_entry) |
| 104 | |
| 105 | # Log to console if enabled |
| 106 | if self.log_to_console: |
| 107 | print(f"[{timestamp}] [{level.value}] " |
| 108 | f"{f'Node {node_id} - ' if node_id else ''}" |
| 109 | f"{f'Event {event_type} - ' if event_type else ''}" |
| 110 | f"{message} " |
| 111 | f"{f'Details: {details} ' if details else ''}" |
| 112 | f"{f'Duration: {duration}' if duration else ''}") |
| 113 | |
| 114 | # Log using structured logger if enabled |
| 115 | if self.use_structured_logging and self.structured_logger: |
| 116 | structured_details = { |
| 117 | "workflow_id": self.workflow_id, |
| 118 | "node_id": node_id, |
| 119 | "event_type": event_type.value if event_type else None, |
| 120 | "execution_path": execution_path, |
| 121 | "duration": duration, |
| 122 | **safe_details |
no outgoing calls
no test coverage detected