<?php
/**
* Extracts runtime environment variables from a legacy project's logs.
*
* This script parses log files for specific patterns indicating runtime
* environment information. It focuses on common variables like PHP version,
* server environment, and loaded extensions.
*
* @param string $logFilePath The path to the log file.
* @return array An associative array containing extracted environment variables.
*/
function extractRuntimeEnvironment(string $logFilePath): array
{
$environment = [];
if (!file_exists($logFilePath)) {
error_log("Log file not found: " . $logFilePath);
return $environment; // Return empty array if file doesn't exist.
}
$logContent = file_get_contents($logFilePath);
if ($logContent === false) {
error_log("Failed to read log file: " . $logFilePath);
return $environment; // Return empty array if file reading fails.
}
// PHP Version
if (preg_match('/PHP ([\d.]+)/', $logContent, $matches)) {
$environment['php_version'] = trim($matches[1]);
}
// Server Environment (e.g., Apache, Nginx)
if (preg_match('/Server: ([\w\s]+)/i', $logContent, $matches)) {
$environment['server'] = trim($matches[1]);
}
// Operating System
if (preg_match('/OS: ([\w\s]+)/i', $logContent, $matches)) {
$environment['os'] = trim($matches[1]);
}
// PHP Extensions
if (preg_match('/Loaded Configuration File: ([\w\s]+)/', $logContent, $matches)) {
$config_file = trim($matches[1]);
if (strpos($config_file, 'php.ini') !== false) {
$ini_content = file_get_contents($config_file);
if ($ini_content !== false) {
preg_match_all('/extension=(.*)/m', $ini_content, $extension_matches);
$extensions = [];
foreach ($extension_matches[1] as $extension) {
$extensions[] = trim($extension);
}
$environment['php_extensions'] = array_unique($extensions);
}
}
}
// Memory Limit
if (preg_match('/memory_limit\s*=\s*(\d+M)/', $logContent, $matches)) {
$environment['memory_limit'] = $matches[1] . 'M';
}
//Error Reporting Level
if (preg_match('/error_reporting\s*=\s*(\d+)/', $logContent, $matches)) {
$environment['error_reporting'] = $matches[1];
}
return $environment;
}
// Example usage:
$logFile = 'path/to/your/log_file.log'; // Replace with the actual log file path
$runtimeEnvironment = extractRuntimeEnvironment($logFile);
print_r($runtimeEnvironment);
?>
Add your comment