1. <?php
  2. /**
  3. * Function to read log files and display errors.
  4. * @param array $logFiles Array of log file paths.
  5. */
  6. function displayLogErrors(array $logFiles): void
  7. {
  8. foreach ($logFiles as $logFile) {
  9. if (file_exists($logFile)) {
  10. try {
  11. $logContent = file_get_contents($logFile); // Read the entire log file content
  12. if ($logContent !== false) {
  13. $errorLines = [];
  14. $lines = explode("\n", $logContent); // Split into lines
  15. foreach ($lines as $line) {
  16. if (strpos($line, 'error') !== false || strpos($line, 'exception') !== false || strpos($line, 'warning') !== false) {
  17. $errorLines[] = $line; // Collect error lines
  18. }
  19. }
  20. if (!empty($errorLines)) {
  21. echo "--- Errors from: " . $logFile . " ---\n";
  22. foreach ($errorLines as $errorLine) {
  23. echo $errorLine . "\n"; // Display each error line
  24. }
  25. } else {
  26. echo "No errors found in: " . $logFile . "\n";
  27. }
  28. } else {
  29. echo "Error reading: " . $logFile . "\n";
  30. }
  31. } catch (Exception $e) {
  32. echo "Error processing " . $logFile . ": " . $e->getMessage() . "\n"; // Handle file reading errors
  33. }
  34. } else {
  35. echo "Log file not found: " . $logFile . "\n"; //Handle file not found errors
  36. }
  37. }
  38. }
  39. // Example usage: Replace with your actual log file paths
  40. $logFiles = [
  41. 'error.log',
  42. 'application.log',
  43. ];
  44. displayLogErrors($logFiles);
  45. ?>

Add your comment