<?php
/**
* Function to nest log file structures for debugging.
*
* @param string $logFilePath The path to the log file.
* @param string $level The log level (e.g., "INFO", "WARNING", "ERROR").
* @param string $message The log message.
* @return string The nested log structure as a string.
*/
function nestLog(string $logFilePath, string $level, string $message): string
{
// Get the filename from the path.
$filename = basename($logFilePath);
// Create the nested structure.
$nestedLog = "└── " . $filename . ": " . $level . " - " . $message;
// Return the nested log structure.
return $nestedLog;
}
/**
* Example usage: Simulates reading and processing a log file,
* and then creating a nested log structure for each entry.
*
* @param string $logFileContent The content of the log file.
* @return string The nested log structure as a single string.
*/
function generateNestedLog(string $logFileContent): string
{
$lines = explode("\n", $logFileContent); // Split the log file content into lines.
$nestedLog = "";
foreach ($lines as $line) {
// Check if the line starts with a specific marker (e.g., "INFO", "WARNING", "ERROR").
if (strpos($line, "INFO:") === 0) {
list($logFilePath, $level, $message) = explode(":", $line);
$nestedLog .= nestLog($logFilePath, $level, trim($message)) . "\n"; //Add the nested log entry.
} elseif (strpos($line, "WARNING:") === 0) {
list($logFilePath, $level, $message) = explode(":", $line);
$nestedLog .= nestLog($logFilePath, $level, trim($message)) . "\n";
} elseif (strpos($line, "ERROR:") === 0) {
list($logFilePath, $level, $message) = explode(":", $line);
$nestedLog .= nestLog($logFilePath, $level, trim($message)) . "\n";
}
}
return $nestedLog;
}
// Example log file content.
$logFileContent = <<<LOG
INFO: /var/log/app.log - Application started successfully.
WARNING: /var/log/app.log - Low disk space detected.
ERROR: /var/log/app.log - Database connection failed.
INFO: /var/log/app.log - User logged in.
LOG
;
// Generate the nested log structure.
$nestedLogString = generateNestedLog($logFileContent);
// Output the nested log structure.
echo $nestedLogString;
?>
Add your comment