1. <?php
  2. /**
  3. * Task Queue Logger for Synchronous Execution
  4. */
  5. class TaskQueueLogger
  6. {
  7. private $logFile = 'task_queue.log';
  8. /**
  9. * Logs a task operation.
  10. *
  11. * @param string $taskName The name of the task.
  12. * @param string $status The status of the task (e.g., 'start', 'success', 'failure', 'error').
  13. * @param mixed $data Optional data related to the task.
  14. */
  15. public function logTaskOperation(string $taskName, string $status, $data = null): void
  16. {
  17. $timestamp = date('Y-m-d H:i:s');
  18. $logMessage = "[$timestamp] Task: $taskName, Status: $status";
  19. if ($data !== null) {
  20. $logMessage .= ", Data: " . json_encode($data);
  21. }
  22. $logMessage .= PHP_EOL;
  23. $this->logToFile($logMessage);
  24. }
  25. /**
  26. * Logs to the log file.
  27. *
  28. * @param string $message The message to log.
  29. */
  30. private function logToFile(string $message): void
  31. {
  32. file_put_contents($this->logFile, $message, FILE_APPEND);
  33. }
  34. /**
  35. * Example usage (can be removed in production)
  36. */
  37. public static function exampleUsage()
  38. {
  39. $logger = new TaskQueueLogger();
  40. $logger->logTaskOperation('ProcessOrder', 'start');
  41. // Simulate a successful task
  42. $logger->logTaskOperation('ProcessOrder', 'success', ['orderId' => 123, 'total' => 50.00]);
  43. // Simulate an error
  44. $logger->logTaskOperation('CalculateInvoice', 'failure', ['reason' => 'Invalid data']);
  45. $logger->logTaskOperation('SendEmail', 'error', ['error' => new Exception('Email sending failed')]);
  46. }
  47. }
  48. //Example Usage (uncomment to run)
  49. //TaskQueueLogger::exampleUsage();
  50. ?>

Add your comment