<?php
/**
* Gracefully handles runtime environment fallback and diagnostics with error logging.
*/
// Define fallback environment variables and their defaults.
$fallback_env = [
'DEBUG' => false,
'LOG_DIR' => 'logs',
'ENVIRONMENT' => 'fallback',
];
// Merge environment variables with defaults.
$_ENV = array_merge($_ENV, $fallback_env);
// Error logging function.
function logError($message) {
global $LOG_DIR;
$timestamp = date('Y-m-d H:i:s');
$logFile = $LOG_DIR . '/' . $timestamp . '.log';
error_log("[$timestamp] ERROR: $message", 3, $logFile); // Log to file
error_log("[$timestamp] ERROR: $message", 3, null); //Also send to standard error stream
}
// Check for environment variables.
if (!isset($_ENV['DEBUG'])) {
$_ENV['DEBUG'] = false; //Default to false if not set
}
if (!isset($_ENV['LOG_DIR'])) {
$_ENV['LOG_DIR'] = 'logs'; //Default log directory
}
if (!isset($_ENV['ENVIRONMENT'])) {
$_ENV['ENVIRONMENT'] = 'fallback'; //Default environment
}
// Diagnostic logging
if ($_ENV['DEBUG']) {
// Log basic environment information.
logError("--- Environment Diagnostics ---");
logError("DEBUG: Environment: " . $_ENV['ENVIRONMENT']);
logError("DEBUG: Debug Mode: " . $_ENV['DEBUG']);
logError("DEBUG: Log Directory: " . $_ENV['LOG_DIR']);
logError("DEBUG: PHP Version: " . phpversion());
logError("DEBUG: Server OS: " . PHP_OS);
logError("DEBUG: Server Architecture: " . PHP_SAPI);
//Example of checking if a specific function exists
if(function_exists('mysqli_connect')){
logError("DEBUG: mysqli_connect function available.");
} else {
logError("DEBUG: mysqli_connect function NOT available.");
}
}
//Example usage: Implement your application logic here.
//If certain features require a specific environment, you can check $_ENV['ENVIRONMENT'] and handle accordingly.
?>
Add your comment