Skip to content

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:

StageStep #Description
WORKFLOW_START0Workflow initialization
PROFILE_RETRIEVAL1Client data retrieval
STATUS_UPDATE2Processing status updates
DATA_PREPARATION3Input data preparation
AI_ORCHESTRATION4Core AI processing
PDF_ENHANCEMENT11Report generation
STORAGE_PROCESSING12Asset storage
WORKFLOW_COMPLETE99Workflow completion

Usage Examples

Basic Workflow Tracking

javascript
// 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

javascript
// 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

javascript
// 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:

json
{
  "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

  1. Navigate to Cloudflare DashboardWorkers & Pages{worker-name}
  2. Click LogsReal-time Logs
  3. Use these search patterns:
Search TermPurpose
WORKFLOW_STARTFind workflow initiations
WORKFLOW_STEPSee step-by-step progression
ENHANCED:Find enhanced orchestrator responses
{trackingId}All logs for specific workflow
executive_summarySpecific processing steps

Log Structure

Console logs follow this structured format:

json
{
  "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:

sql
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

javascript
// Use descriptive, unique tracking IDs
const trackingId = `PROD_${Date.now()}_${clientId.substring(0, 8)}`;

2. Metadata Usage

javascript
// 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

javascript
// 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:

javascript
const workflowLogger = new WorkflowLogger(trackingId, env, { debug: true });

Integration with UI

Real-time Progress Tracking

javascript
// 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

javascript
// 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+

Strategic Intelligence Hub Documentation