WorkflowLogger Implementation Guide
Overview
The WorkflowLogger is an enterprise-grade logging and tracking system designed for AI orchestration workflows in Cloudflare Workers. It provides comprehensive step-by-step visibility, real-time monitoring, and historical tracking for complex business analysis processes.
Key Features
- Real-time Console Logging: Structured JSON logs visible in Cloudflare Dashboard
- Database Persistence: Historical workflow data stored in D1 Database
- Step-by-Step Tracking: Detailed progression through workflow stages
- Performance Metrics: Duration tracking, token usage, and error monitoring
- API Integration: Status endpoints for UI monitoring and dashboard integration
- Non-blocking Architecture: Logging failures don't interrupt main workflow
Core Components
1. WorkflowLogger Class (src/utils/WorkflowLogger.js)
The main logging interface that handles:
- Workflow initialization and completion
- Step-by-step progress tracking
- Performance metrics collection
- Database persistence (when schema is available)
2. Enhanced AdminHandlerRoutes (src/handlers/AdminHandlerRoutes.js)
Integrates WorkflowLogger into the orchestration process:
- Automatic workflow tracking for admin operations
- Enhanced response format with diagnostic data
- Real-time step progression logging
3. Status API (/api/admin/status/{trackingId})
Provides UI integration capabilities:
- Current workflow status and progress
- Historical processing logs
- Asset download links
- Processing metrics and error tracking
Implementation Architecture
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ UI Frontend │────│ Status API │────│ Database │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│
┌──────────────────┐ ┌─────────────────┐
│ AdminHandlerRoutes│────│ WorkflowLogger │
└──────────────────┘ └─────────────────┘
│ │
┌──────────────────┐ ┌─────────────────┐
│ AI Orchestration │────│ Console Logs │
│ Workflow │ │ (Cloudflare │
└──────────────────┘ │ Dashboard) │
└─────────────────┘Workflow Stages
The WorkflowLogger tracks these standard workflow stages:
| Stage | Step # | Description |
|---|---|---|
WORKFLOW_START | 0 | Workflow initialization |
PROFILE_RETRIEVAL | 1 | Client data retrieval |
STATUS_UPDATE | 2 | Processing status updates |
DATA_PREPARATION | 3 | Input data preparation |
AI_ORCHESTRATION | 4 | Core AI processing |
PDF_ENHANCEMENT | 11 | Report generation |
STORAGE_PROCESSING | 12 | Asset storage |
WORKFLOW_COMPLETE | 99 | Workflow completion |
Usage Examples
Basic Workflow Tracking
// Initialize WorkflowLogger
const workflowLogger = new WorkflowLogger(trackingId, env);
// Start workflow
await workflowLogger.logWorkflowStart(8, 'healthcare', 'comprehensive', {
custom_metadata: 'value'
});
// Track individual steps
await workflowLogger.logStep('executive_summary', 1, 'started');
// ... perform processing ...
await workflowLogger.logStep('executive_summary', 1, 'completed', {
duration: 2500,
tokens: 245
});
// Complete workflow
await workflowLogger.logWorkflowComplete(totalTokens, {
summary: 'Processing completed successfully'
});Using Step Timers
// Create and use step timer for automatic duration tracking
const timer = workflowLogger.createStepTimer('ai_analysis', 4);
await timer.start({
model: '@cf/meta/llama-3.1-8b-instruct',
prompt_length: 1250
});
// ... perform AI processing ...
await timer.complete({
tokens_generated: 456,
quality_score: 0.94
});Enhanced AdminHandlerRoutes Integration
// WorkflowLogger is automatically integrated in AdminHandlerRoutes
const response = await AdminHandlerRoutes.executeOrchestrator(request, env, ctx);
// Response includes enhanced format:
{
"success": true,
"message": "ENHANCED: Orchestrator execution completed successfully",
"diagnostic": {
"enhancedAdminHandlerUsed": true,
"workflowLoggerActive": true,
"version": "enhanced-v1.0"
},
"workflowTracking": {
"totalSteps": 12,
"completedSteps": 12,
"totalDuration": 32228
}
}API Endpoints
Status Monitoring
GET /api/admin/status/{trackingId}
Returns comprehensive workflow status:
{
"success": true,
"trackingId": "M4A4SSPV",
"status": "completed",
"data": {
"submission": { /* Complete client data */ },
"logs": [ /* Processing history */ ],
"processing_metrics": {
"progress_percentage": 100,
"completed_steps": 12,
"total_steps": 12,
"current_step": null
},
"downloadLinks": [ /* Asset URLs */ ]
}
}Testing Endpoints
POST /test-workflow-logger
- Comprehensive WorkflowLogger functionality test
- Generates sample workflow with all stages
- Returns detailed test results and dashboard search tips
POST /test-enhanced-orchestrator
- Tests enhanced AdminHandlerRoutes with WorkflowLogger
- Demonstrates real workflow integration
- Shows enhanced response format
Cloudflare Dashboard Integration
Viewing Logs
- Navigate to Cloudflare Dashboard → Workers & Pages → {worker-name}
- Click Logs → Real-time Logs
- Use these search patterns:
| Search Term | Purpose |
|---|---|
WORKFLOW_START | Find workflow initiations |
WORKFLOW_STEP | See step-by-step progression |
ENHANCED: | Find enhanced orchestrator responses |
{trackingId} | All logs for specific workflow |
executive_summary | Specific processing steps |
Log Structure
Console logs follow this structured format:
{
"message": "🔄 WORKFLOW_4_AI_ORCHESTRATION",
"level": "INFO",
"data": {
"tracking_id": "M4A4SSPV",
"step_name": "ai_orchestration",
"step_number": 4,
"status": "started",
"metadata": { /* Custom data */ }
},
"timestamp": "2025-08-10T23:27:21.307Z"
}Database Schema
The WorkflowLogger uses the orchestrator_processing_log table:
CREATE TABLE orchestrator_processing_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tracking_id TEXT NOT NULL,
step_name TEXT,
step_number INTEGER,
status TEXT NOT NULL,
duration_ms INTEGER,
tokens_used INTEGER,
error_message TEXT,
metadata TEXT, -- JSON string
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);Schema Migration
If database persistence shows "Failed to persist workflow log", check schema compatibility:
GET /health?schema=check
This endpoint analyzes the current database schema and provides migration recommendations.
Error Handling
The WorkflowLogger implements non-blocking error handling:
- Console logging always succeeds (visible in Cloudflare Dashboard)
- Database persistence failures are logged but don't interrupt workflow
- API responses include diagnostic information about logging status
- Graceful degradation ensures main workflow continues even with logging issues
Performance Considerations
- Minimal Overhead: Logging operations are optimized for speed
- Async Operations: Database writes don't block main workflow
- Selective Logging: Only essential steps are tracked by default
- Structured Data: JSON format enables efficient searching and filtering
Best Practices
1. Tracking ID Convention
// Use descriptive, unique tracking IDs
const trackingId = `PROD_${Date.now()}_${clientId.substring(0, 8)}`;2. Metadata Usage
// Include relevant context in metadata
await workflowLogger.logStep('ai_analysis', 4, 'completed', {
duration: processingTime,
tokens: tokenCount,
model_used: 'llama-3.1-8b',
quality_score: 0.95,
custom_context: {
industry: 'healthcare',
complexity: 'high'
}
});3. Error Logging
// Log errors with context
await workflowLogger.logStep('pdf_generation', 11, 'failed', {
error: error.message,
error_type: error.constructor.name,
retry_count: retryAttempt,
context: 'PDF template processing'
});Troubleshooting
Common Issues
1. Logs not appearing in Dashboard
- Check if WorkflowLogger is properly initialized
- Verify tracking ID is being passed correctly
- Ensure console logging permissions are enabled
2. Database persistence failing
- Run schema check:
GET /health?schema=check - Check D1 database binding configuration
- Verify table exists and has correct columns
3. Step tracking incomplete
- Ensure all steps call both 'started' and 'completed' status
- Check for uncaught errors interrupting workflow
- Verify step numbers are sequential and unique
Debug Mode
Enable debug mode for additional logging:
const workflowLogger = new WorkflowLogger(trackingId, env, { debug: true });Integration with UI
Real-time Progress Tracking
// Poll status API for real-time updates
const pollStatus = async (trackingId) => {
const response = await fetch(`/api/admin/status/${trackingId}`);
const status = await response.json();
// Update UI progress bar
updateProgress(status.processing_metrics.progress_percentage);
// Show current step
displayCurrentStep(status.processing_metrics.current_step);
// Handle completion
if (status.status === 'completed') {
showDownloadLinks(status.downloadLinks);
}
};
// Poll every 2 seconds during processing
const interval = setInterval(() => pollStatus(trackingId), 2000);Error Display
// Show processing errors to users
if (status.data.logs.some(log => log.error_message)) {
const errors = status.data.logs
.filter(log => log.error_message)
.map(log => ({
step: log.step_name,
error: log.error_message,
timestamp: log.timestamp
}));
displayErrors(errors);
}Conclusion
The WorkflowLogger provides enterprise-grade visibility into complex AI orchestration workflows. With real-time monitoring, comprehensive logging, and seamless UI integration, it enables confident deployment and management of sophisticated business analysis systems.
For additional support or feature requests, refer to the codebase documentation or contact the development team.
Version: 1.0
Last Updated: August 2025
Compatibility: Cloudflare Workers, D1 Database, Wrangler 4.x+