<?php
/**
* Pretty-prints log file contents with basic input validation.
*
* @param string $logFilePath Path to the log file.
* @return void
*/
function prettyPrintLog(string $logFilePath): void
{
//Basic input validation
if (!file_exists($logFilePath)) {
echo "Error: Log file not found at '$logFilePath'\n";
return;
}
if (!is_readable($logFilePath)) {
echo "Error: Log file is not readable.\n";
return;
}
$logContent = file_get_contents($logFilePath);
if ($logContent === false) {
echo "Error: Failed to read log file.\n";
return;
}
// Split the log content into lines
$logLines = explode("\n", $logContent);
// Pretty print each log line
foreach ($logLines as $lineNumber => $logLine) {
//Clean up the line by removing leading/trailing whitespace
$logLine = trim($logLine);
if ($logLine !== "") {
echo sprintf("[%03d] %s\n", $lineNumber, $logLine); // Format with line number
}
}
}
//Example Usage (replace with your log file path)
if (isset($argv[1])) {
prettyPrintLog($argv[1]);
} else {
echo "Usage: php pretty_print_log.php <log_file_path>\n";
}
?>
Add your comment