1. <?php
  2. /**
  3. * Function to nest log file structures for debugging.
  4. *
  5. * @param string $logFilePath The path to the log file.
  6. * @param string $level The log level (e.g., "INFO", "WARNING", "ERROR").
  7. * @param string $message The log message.
  8. * @return string The nested log structure as a string.
  9. */
  10. function nestLog(string $logFilePath, string $level, string $message): string
  11. {
  12. // Get the filename from the path.
  13. $filename = basename($logFilePath);
  14. // Create the nested structure.
  15. $nestedLog = "└── " . $filename . ": " . $level . " - " . $message;
  16. // Return the nested log structure.
  17. return $nestedLog;
  18. }
  19. /**
  20. * Example usage: Simulates reading and processing a log file,
  21. * and then creating a nested log structure for each entry.
  22. *
  23. * @param string $logFileContent The content of the log file.
  24. * @return string The nested log structure as a single string.
  25. */
  26. function generateNestedLog(string $logFileContent): string
  27. {
  28. $lines = explode("\n", $logFileContent); // Split the log file content into lines.
  29. $nestedLog = "";
  30. foreach ($lines as $line) {
  31. // Check if the line starts with a specific marker (e.g., "INFO", "WARNING", "ERROR").
  32. if (strpos($line, "INFO:") === 0) {
  33. list($logFilePath, $level, $message) = explode(":", $line);
  34. $nestedLog .= nestLog($logFilePath, $level, trim($message)) . "\n"; //Add the nested log entry.
  35. } elseif (strpos($line, "WARNING:") === 0) {
  36. list($logFilePath, $level, $message) = explode(":", $line);
  37. $nestedLog .= nestLog($logFilePath, $level, trim($message)) . "\n";
  38. } elseif (strpos($line, "ERROR:") === 0) {
  39. list($logFilePath, $level, $message) = explode(":", $line);
  40. $nestedLog .= nestLog($logFilePath, $level, trim($message)) . "\n";
  41. }
  42. }
  43. return $nestedLog;
  44. }
  45. // Example log file content.
  46. $logFileContent = <<<LOG
  47. INFO: /var/log/app.log - Application started successfully.
  48. WARNING: /var/log/app.log - Low disk space detected.
  49. ERROR: /var/log/app.log - Database connection failed.
  50. INFO: /var/log/app.log - User logged in.
  51. LOG
  52. ;
  53. // Generate the nested log structure.
  54. $nestedLogString = generateNestedLog($logFileContent);
  55. // Output the nested log structure.
  56. echo $nestedLogString;
  57. ?>

Add your comment