[Galactica] New log_limit option

This commit is contained in:
Andrés Montañez 2022-04-10 19:38:43 -03:00
parent a370c1c0c9
commit 2783292899
No known key found for this signature in database
GPG key ID: 97E9F675F4C03DE2

View file

@ -24,6 +24,7 @@ use Symfony\Component\Console\Application;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException;
use Mage\Runtime\Exception\RuntimeException;
use Symfony\Component\Filesystem\Filesystem;
/**
* The Console Application for launching the Mage command in a standalone instance
@ -89,6 +90,9 @@ class MageApplication extends Application
$logger = new Logger('magephp');
$logger->pushHandler(new StreamHandler($logfile));
$logLimit = isset($config['magephp']['log_limit']) ? intval($config['magephp']['log_limit']) : 30;
$this->clearOldLogs($config['magephp']['log_dir'], $logLimit);
} elseif (array_key_exists('log_dir', $config['magephp']) && !is_dir($config['magephp']['log_dir'])) {
throw new RuntimeException(
sprintf(
@ -108,6 +112,24 @@ class MageApplication extends Application
);
}
protected function clearOldLogs(string $logDir, int $logLimit): void
{
$filesystem = new Filesystem();
$finder = new Finder();
$finder
->files()
->followLinks()
->in($logDir)
->name('*.log')
->sortByModifiedTime()
->reverseSorting();
$logs = iterator_to_array($finder);
$logsToRemove = array_slice($logs, $logLimit - 1);
$filesystem->remove($logsToRemove);
}
/**
* Loads the BuiltIn Commands
*/