Skip to content

Commit e0c4681

Browse files
committed
New command for log retention
1 parent 62cc86f commit e0c4681

File tree

7 files changed

+124
-2
lines changed

7 files changed

+124
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.2
2+
3+
- New command for log retention
4+
15
## 1.2.1
26

37
- Country added in logs grid

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ Display Audit Trails under the menu: *Advanced settings > Sucuri Logs*
5757

5858
- **Refresh:** Add the logs for the current day
5959

60-
### CLI
60+
## CLI
61+
62+
### Refresh Logs
6163

6264
```shell
6365
./bin/console sucuri:log-refresh
@@ -67,4 +69,23 @@ It's recommended to refresh the logs hourly using a CRON job.
6769

6870
```php
6971
0 * * * * /path/to/prestashop/bin/console sucuri:log-refresh
72+
```
73+
74+
### Clean Logs
75+
76+
```shell
77+
./bin/console sucuri:log-clean {retention_period}
78+
```
79+
80+
Keep logs for the specified number of days in command argument. Default period is 60 days.
81+
82+
```shell
83+
./bin/console sucuri:log-clean 30
84+
158 logs purged for a 30-day retention period
85+
```
86+
87+
It's recommended to clean the logs hourly using a CRON job.
88+
89+
```php
90+
0 * * * * /path/to/prestashop/bin/console sucuri:log-clean 60
7091
```

config/services.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,12 @@ services:
113113
$translator: '@translator'
114114
tags:
115115
- { name: 'console.command' }
116+
117+
pixel.sucuri.log.clean:
118+
class: Pixel\Module\Sucuri\Command\LogClean
119+
public: true
120+
arguments:
121+
$api: '@pixel.sucuri.api'
122+
$translator: '@translator'
123+
tags:
124+
- { name: 'console.command' }

pixel_sucuri.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Pixel_sucuri extends Module
1717
public function __construct()
1818
{
1919
$this->name = 'pixel_sucuri';
20-
$this->version = '1.2.1';
20+
$this->version = '1.2.2';
2121
$this->author = 'Pixel Open';
2222
$this->tab = 'administration';
2323
$this->need_instance = 0;

src/Command/LogClean.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Pixel\Module\Sucuri\Command;
4+
5+
use Exception;
6+
use Pixel\Module\Sucuri\Model\Api;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Translation\TranslatorInterface;
12+
13+
class LogClean extends Command
14+
{
15+
private Api $api;
16+
17+
private TranslatorInterface $translator;
18+
19+
public function __construct(
20+
Api $api,
21+
TranslatorInterface $translator,
22+
string $name = null
23+
) {
24+
$this->api = $api;
25+
$this->translator = $translator;
26+
27+
parent::__construct($name);
28+
}
29+
30+
protected function configure()
31+
{
32+
$this->setName('sucuri:log-clean')
33+
->setDescription('Clean Sucuri logs')
34+
->addArgument('retention', InputArgument::OPTIONAL, 'Log retention (days)', 60);
35+
}
36+
37+
protected function execute(InputInterface $input, OutputInterface $output)
38+
{
39+
try {
40+
$retention = (int)$input->getArgument('retention');
41+
if ($retention) {
42+
$total = $this->api->cleanLog($retention);
43+
$output->writeln(
44+
$this->translator->trans('%s logs purged for a %s-day retention period', [$total, $retention], 'Modules.Pixelsucuri.Admin')
45+
);
46+
}
47+
} catch (Exception $exception) {
48+
$output->writeln(
49+
$this->translator->trans('Unable to clean the logs: %s', [$exception->getMessage()], 'Modules.Pixelsucuri.Admin')
50+
);
51+
}
52+
}
53+
}

src/Model/Api.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Pixel\Module\Sucuri\Model;
1010

11+
use Doctrine\Common\Collections\Criteria;
1112
use Exception;
1213
use Pixel\Module\Sucuri\Helper\Cache;
1314
use Pixel\Module\Sucuri\Helper\Config;
@@ -236,6 +237,28 @@ protected function doRefreshLog(int $offset = 0): int
236237
return $total;
237238
}
238239

240+
/**
241+
* Clean logs
242+
*
243+
* @param int $retentionPeriod
244+
* @return void
245+
* @throws Exception
246+
*/
247+
public function cleanLog(int $retentionPeriod = 60): int
248+
{
249+
$toDate = date('Y-m-d H:i:s', strtotime('-' . $retentionPeriod . ' days'));
250+
251+
$criteria = Criteria::create()->where(Criteria::expr()->lt('fullDate', $toDate));
252+
253+
$logs = $this->logRepository->matching($criteria);
254+
255+
foreach ($logs as $log) {
256+
$this->logRepository->delete($log->getId());
257+
}
258+
259+
return count($logs);
260+
}
261+
239262
/**
240263
* Is option editable
241264
*

src/Repository/SucuriLog.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Pixel\Module\Sucuri\Repository;
1010

11+
use Doctrine\Common\Collections\Criteria;
1112
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
1213
use Doctrine\ORM\EntityManager;
1314
use Doctrine\ORM\OptimisticLockException;
@@ -169,6 +170,17 @@ public function findAll(array $criteria, ?array $orderBy = null): array
169170
return $repository->findBy($criteria, $orderBy);
170171
}
171172

173+
/**
174+
* @param Criteria $criteria
175+
* @return array
176+
*/
177+
public function matching(Criteria $criteria): array
178+
{
179+
$repository = $this->entityManager->getRepository(Entity::class);
180+
181+
return $repository->matching($criteria)->getValues();
182+
}
183+
172184
/**
173185
* @throws Exception
174186
*/

0 commit comments

Comments
 (0)