1. <?php
  2. /**
  3. * Extracts runtime environment variables from a legacy project's logs.
  4. *
  5. * This script parses log files for specific patterns indicating runtime
  6. * environment information. It focuses on common variables like PHP version,
  7. * server environment, and loaded extensions.
  8. *
  9. * @param string $logFilePath The path to the log file.
  10. * @return array An associative array containing extracted environment variables.
  11. */
  12. function extractRuntimeEnvironment(string $logFilePath): array
  13. {
  14. $environment = [];
  15. if (!file_exists($logFilePath)) {
  16. error_log("Log file not found: " . $logFilePath);
  17. return $environment; // Return empty array if file doesn't exist.
  18. }
  19. $logContent = file_get_contents($logFilePath);
  20. if ($logContent === false) {
  21. error_log("Failed to read log file: " . $logFilePath);
  22. return $environment; // Return empty array if file reading fails.
  23. }
  24. // PHP Version
  25. if (preg_match('/PHP ([\d.]+)/', $logContent, $matches)) {
  26. $environment['php_version'] = trim($matches[1]);
  27. }
  28. // Server Environment (e.g., Apache, Nginx)
  29. if (preg_match('/Server: ([\w\s]+)/i', $logContent, $matches)) {
  30. $environment['server'] = trim($matches[1]);
  31. }
  32. // Operating System
  33. if (preg_match('/OS: ([\w\s]+)/i', $logContent, $matches)) {
  34. $environment['os'] = trim($matches[1]);
  35. }
  36. // PHP Extensions
  37. if (preg_match('/Loaded Configuration File: ([\w\s]+)/', $logContent, $matches)) {
  38. $config_file = trim($matches[1]);
  39. if (strpos($config_file, 'php.ini') !== false) {
  40. $ini_content = file_get_contents($config_file);
  41. if ($ini_content !== false) {
  42. preg_match_all('/extension=(.*)/m', $ini_content, $extension_matches);
  43. $extensions = [];
  44. foreach ($extension_matches[1] as $extension) {
  45. $extensions[] = trim($extension);
  46. }
  47. $environment['php_extensions'] = array_unique($extensions);
  48. }
  49. }
  50. }
  51. // Memory Limit
  52. if (preg_match('/memory_limit\s*=\s*(\d+M)/', $logContent, $matches)) {
  53. $environment['memory_limit'] = $matches[1] . 'M';
  54. }
  55. //Error Reporting Level
  56. if (preg_match('/error_reporting\s*=\s*(\d+)/', $logContent, $matches)) {
  57. $environment['error_reporting'] = $matches[1];
  58. }
  59. return $environment;
  60. }
  61. // Example usage:
  62. $logFile = 'path/to/your/log_file.log'; // Replace with the actual log file path
  63. $runtimeEnvironment = extractRuntimeEnvironment($logFile);
  64. print_r($runtimeEnvironment);
  65. ?>

Add your comment