Feat: Add command to flush logs
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user