<?php
/**
* Generates log stream configurations for a legacy project based on a configuration file.
*
* @param string $configFilePath Path to the configuration file.
* @return array An array of log stream configurations. Returns an empty array on error.
*/
function generateLogStreamConfigs(string $configFilePath): array
{
if (!file_exists($configFilePath)) {
error_log("Error: Configuration file not found: $configFilePath");
return [];
}
$config = json_decode(file_get_contents($configFilePath), true);
if ($config === null) {
error_log("Error: Failed to decode JSON from configuration file: $configFilePath");
return [];
}
$logStreamConfigs = [];
if (isset($config['log_streams']) && is_array($config['log_streams'])) {
foreach ($config['log_streams'] as $streamConfig) {
if (is_array($streamConfig)) {
$logStreamConfigs[] = [
'name' => $streamConfig['name'] ?? 'default', // Get stream name, default to 'default' if missing
'level' => $streamConfig['level'] ?? 'info', // Get log level, default to 'info'
'target' => $streamConfig['target'] ?? 'file', // Get target, default to 'file'
'file' => $streamConfig['file'] ?? null, // Path to file, can be null
'format' => $streamConfig['format'] ?? '%time %level %message%', //Log format
'max_size' => $streamConfig['max_size'] ?? 1024 * 1024, //Max file size in bytes
'max_files' => $streamConfig['max_files'] ?? 5, //Max number of files to rotate
];
} else {
error_log("Warning: Invalid log stream configuration found: " . json_encode($streamConfig));
}
}
} else {
error_log("Warning: 'log_streams' section not found or is not an array in config file: $configFilePath");
}
return $logStreamConfigs;
}
//Example usage:
/*
$configs = generateLogStreamConfigs('config.json');
if (!empty($configs)) {
echo "<pre>";
print_r($configs);
echo "</pre>";
} else {
echo "No log stream configurations generated.";
}
*/
?>
Add your comment