Feat: Add command to flush logs

This commit is contained in:
Björn Fromme
2023-10-21 17:45:12 +02:00
parent 2385bccb91
commit 38d6fafcb2
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace App\Command;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:flush-logs',
description: 'Deletes historic log entries',
)]
class FlushLogsCommand extends Command
{
public function __construct(
private readonly Connection $connection,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$dueDate = (new \DateTimeImmutable())->modify('-30 days');
try {
$result = $this
->connection
->executeQuery('DELETE FROM log WHERE log.timestamp < ?', [$dueDate->format('Y-m-d H:i:s')]);
$io->info('Deleted '.$result->rowCount().' log entries');
} catch (Exception $e) {
$io->error('Unable to delete log entries: '.$e->getMessage());
}
return Command::SUCCESS;
}
}