From 360518b90554e46dd6f3f995d6e2f032633f9555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Sat, 14 Sep 2024 19:35:37 +0200 Subject: [PATCH] fix: Replace deprecated ILogger with Psr\Log\LoggerInterface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/BackgroundJob/TrainJobIpV4.php | 28 ++++---------- lib/BackgroundJob/TrainJobIpV6.php | 28 ++++---------- lib/Listener/LoginMailListener.php | 35 +++++------------- lib/Listener/LoginNotificationListener.php | 23 ++++-------- lib/Service/ETLService.php | 22 +++-------- lib/Service/EstimatorService.php | 16 +++----- lib/Service/LoginClassifier.php | 43 +++++----------------- lib/Service/ModelStore.php | 40 +++++--------------- 8 files changed, 62 insertions(+), 173 deletions(-) diff --git a/lib/BackgroundJob/TrainJobIpV4.php b/lib/BackgroundJob/TrainJobIpV4.php index f61c418..99bab3f 100644 --- a/lib/BackgroundJob/TrainJobIpV4.php +++ b/lib/BackgroundJob/TrainJobIpV4.php @@ -15,20 +15,16 @@ use OCA\SuspiciousLogin\Service\TrainingDataConfig; use OCA\SuspiciousLogin\Service\TrainService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Throwable; class TrainJobIpV4 extends TimedJob { - /** @var TrainService */ - private $trainService; - - /** @var ILogger */ - private $logger; - - public function __construct(TrainService $trainService, - ILogger $logger, - ITimeFactory $time) { + public function __construct( + private TrainService $trainService, + private LoggerInterface $logger, + ITimeFactory $time, + ) { parent::__construct($time); $this->setInterval(24 * 60 * 60); @@ -38,8 +34,6 @@ class TrainJobIpV4 extends TimedJob { if (defined('\OCP\BackgroundJob\IJob::TIME_INSENSITIVE') && method_exists($this, 'setTimeSensitivity')) { $this->setTimeSensitivity(self::TIME_INSENSITIVE); } - $this->trainService = $trainService; - $this->logger = $logger; } /** @@ -56,15 +50,9 @@ class TrainJobIpV4 extends TimedJob { $strategy ); } catch (InsufficientDataException $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::INFO, - 'message' => 'No suspicious login model for IPv4 trained because of insufficient data', - ]); + $this->logger->info('No suspicious login model for IPv4 trained because of insufficient data', ['exception' => $ex]); } catch (Throwable $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::ERROR, - 'message' => 'Caught unknown error during IPv4 background training', - ]); + $this->logger->error('Caught unknown error during IPv4 background training', ['exception' => $ex]); } } } diff --git a/lib/BackgroundJob/TrainJobIpV6.php b/lib/BackgroundJob/TrainJobIpV6.php index bbb348d..bf236d0 100644 --- a/lib/BackgroundJob/TrainJobIpV6.php +++ b/lib/BackgroundJob/TrainJobIpV6.php @@ -15,20 +15,16 @@ use OCA\SuspiciousLogin\Service\TrainingDataConfig; use OCA\SuspiciousLogin\Service\TrainService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Throwable; class TrainJobIpV6 extends TimedJob { - /** @var TrainService */ - private $trainService; - - /** @var ILogger */ - private $logger; - - public function __construct(TrainService $trainService, - ILogger $logger, - ITimeFactory $time) { + public function __construct( + private TrainService $trainService, + private LoggerInterface $logger, + ITimeFactory $time, + ) { parent::__construct($time); $this->setInterval(24 * 60 * 60); @@ -38,8 +34,6 @@ class TrainJobIpV6 extends TimedJob { if (defined('\OCP\BackgroundJob\IJob::TIME_INSENSITIVE') && method_exists($this, 'setTimeSensitivity')) { $this->setTimeSensitivity(self::TIME_INSENSITIVE); } - $this->trainService = $trainService; - $this->logger = $logger; } /** @@ -56,15 +50,9 @@ class TrainJobIpV6 extends TimedJob { $strategy ); } catch (InsufficientDataException $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::INFO, - 'message' => 'No suspicious login model for IPv6 trained because of insufficient data', - ]); + $this->logger->info('No suspicious login model for IPv6 trained because of insufficient data', ['exception' => $ex]); } catch (Throwable $ex) { - $this->logger->logException($ex, [ - 'level' => ILogger::ERROR, - 'message' => 'Caught unknown error during IPv6 background training', - ]); + $this->logger->error('Caught unknown error during IPv6 background training', ['exception' => $ex]); } } } diff --git a/lib/Listener/LoginMailListener.php b/lib/Listener/LoginMailListener.php index 659b0cf..5ad72a0 100644 --- a/lib/Listener/LoginMailListener.php +++ b/lib/Listener/LoginMailListener.php @@ -14,38 +14,24 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\IConfig; use OCP\IL10N; -use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; use OCP\Mail\IMailer; use OCP\Mail\IMessage; +use Psr\Log\LoggerInterface; /** * @implements IEventListener */ class LoginMailListener implements IEventListener { - /** @var ILogger */ - private $logger; - - /** @var IMailer */ - private $mailer; - - /** @var IUserManager */ - private $userManager; - - /** @var IL10N */ - private $l; - - /** @var IConfig */ - protected $config; - - public function __construct(ILogger $logger, IMailer $mailer, IUserManager $userManager, IL10N $l, IConfig $config) { - $this->logger = $logger; - $this->mailer = $mailer; - $this->userManager = $userManager; - $this->l = $l; - $this->config = $config; + public function __construct( + private LoggerInterface $logger, + private IMailer $mailer, + private IUserManager $userManager, + private IL10N $l, + private IConfig $config, + ) { } public function handle(Event $event): void { @@ -69,10 +55,7 @@ class LoginMailListener implements IEventListener { $this->getMail($event, $user) ); } catch (Exception $e) { - $this->logger->logException($e, [ - 'message' => "Could not send suspicious login email to <$uid>", - 'level' => ILogger::ERROR, - ]); + $this->logger->error("Could not send suspicious login email to <$uid>", ['exception' => $e]); } } diff --git a/lib/Listener/LoginNotificationListener.php b/lib/Listener/LoginNotificationListener.php index 639f322..74ff847 100644 --- a/lib/Listener/LoginNotificationListener.php +++ b/lib/Listener/LoginNotificationListener.php @@ -13,27 +13,19 @@ use OCA\SuspiciousLogin\Event\SuspiciousLoginEvent; use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\ILogger; use OCP\Notification\IManager; +use Psr\Log\LoggerInterface; /** * @implements IEventListener */ class LoginNotificationListener implements IEventListener { - /** @var IManager */ - private $notificationManager; - /** @var ITimeFactory */ - private $timeFactory; - /** @var ILogger */ - private $logger; - - public function __construct(IManager $notificationManager, - ITimeFactory $timeFactory, - ILogger $logger) { - $this->notificationManager = $notificationManager; - $this->timeFactory = $timeFactory; - $this->logger = $logger; + public function __construct( + private IManager $notificationManager, + private ITimeFactory $timeFactory, + private LoggerInterface $logger, + ) { } public function handle(Event $event): void { @@ -52,8 +44,7 @@ class LoginNotificationListener implements IEventListener { ]); $this->notificationManager->notify($notification); } catch (\Throwable $ex) { - $this->logger->critical("could not send notification about a suspicious login"); - $this->logger->logException($ex); + $this->logger->critical('Could not send notification about a suspicious login', ['exception' => $ex]); } } } diff --git a/lib/Service/ETLService.php b/lib/Service/ETLService.php index 8f38840..c99cc18 100644 --- a/lib/Service/ETLService.php +++ b/lib/Service/ETLService.php @@ -13,28 +13,18 @@ use Generator; use OCA\SuspiciousLogin\Db\LoginAddressAggregatedMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Output\OutputInterface; class ETLService { public const MAX_BATCH_SIZE = 10000; - /** @var IDBConnection */ - private $db; - - /** @var LoginAddressAggregatedMapper */ - private $aggregatedMapper; - - /** @var ILogger */ - private $logger; - - public function __construct(IDBConnection $db, - LoginAddressAggregatedMapper $aggregatedMapper, - ILogger $logger) { - $this->db = $db; - $this->aggregatedMapper = $aggregatedMapper; - $this->logger = $logger; + public function __construct( + private IDBConnection $db, + private LoginAddressAggregatedMapper $aggregatedMapper, + private LoggerInterface $logger, + ) { } private function getRaw(int $max, ?OutputInterface $output = null): Generator { diff --git a/lib/Service/EstimatorService.php b/lib/Service/EstimatorService.php index 0becd13..ada3f45 100644 --- a/lib/Service/EstimatorService.php +++ b/lib/Service/EstimatorService.php @@ -10,22 +10,16 @@ declare(strict_types=1); namespace OCA\SuspiciousLogin\Service; use OCA\SuspiciousLogin\Exception\ServiceException; -use OCP\ILogger; +use Psr\Log\LoggerInterface; use Rubix\ML\Datasets\Unlabeled; use RuntimeException; class EstimatorService { - /** @var ModelStore */ - private $modelStore; - - /** @var ILogger */ - private $logger; - - public function __construct(ModelStore $modelStore, - ILogger $logger) { - $this->modelStore = $modelStore; - $this->logger = $logger; + public function __construct( + private ModelStore $modelStore, + private LoggerInterface $logger, + ) { } /** diff --git a/lib/Service/LoginClassifier.php b/lib/Service/LoginClassifier.php index 44dd4fb..47d3a74 100644 --- a/lib/Service/LoginClassifier.php +++ b/lib/Service/LoginClassifier.php @@ -16,8 +16,8 @@ use OCA\SuspiciousLogin\Exception\ServiceException; use OCA\SuspiciousLogin\Util\AddressClassifier; use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; -use OCP\ILogger; use OCP\IRequest; +use Psr\Log\LoggerInterface; use Throwable; use function base64_decode; use function explode; @@ -27,36 +27,14 @@ use function substr; class LoginClassifier { - /** @var EstimatorService */ - private $estimator; - - /** @var IRequest */ - private $request; - - /** @var ILogger */ - private $logger; - - /** @var SuspiciousLoginMapper */ - private $mapper; - - /** @var ITimeFactory */ - private $timeFactory; - - /** @var IEventDispatcher */ - private $dispatcher; - - public function __construct(EstimatorService $estimator, - IRequest $request, - ILogger $logger, - SuspiciousLoginMapper $mapper, - ITimeFactory $timeFactory, - IEventDispatcher $dispatcher) { - $this->estimator = $estimator; - $this->request = $request; - $this->logger = $logger; - $this->mapper = $mapper; - $this->timeFactory = $timeFactory; - $this->dispatcher = $dispatcher; + public function __construct( + private EstimatorService $estimator, + private IRequest $request, + private LoggerInterface $logger, + private SuspiciousLoginMapper $mapper, + private ITimeFactory $timeFactory, + private IEventDispatcher $dispatcher, + ) { } /** @@ -126,8 +104,7 @@ class LoginClassifier { return $entity; } catch (Throwable $ex) { - $this->logger->critical("could not save the details of a suspicious login"); - $this->logger->logException($ex); + $this->logger->critical('could not save the details of a suspicious login', ['exception' => $ex]); return null; } } diff --git a/lib/Service/ModelStore.php b/lib/Service/ModelStore.php index 3f1c123..f237e90 100644 --- a/lib/Service/ModelStore.php +++ b/lib/Service/ModelStore.php @@ -18,8 +18,8 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\ICacheFactory; -use OCP\ILogger; use OCP\ITempManager; +use Psr\Log\LoggerInterface; use Rubix\ML\Estimator; use Rubix\ML\Persistable; use Rubix\ML\Persisters\Filesystem; @@ -32,36 +32,14 @@ use function strlen; class ModelStore { public const APPDATA_MODELS_FOLDER = 'models'; - /** @var ModelMapper */ - private $modelMapper; - - /** @var IAppData */ - private $appData; - - /** @var IAppManager */ - private $appManager; - - /** @var ITempManager */ - private $tempManager; - - /** @var ICacheFactory */ - private $cacheFactory; - - /** @var ILogger */ - private $logger; - - public function __construct(ModelMapper $modelMapper, - IAppData $appData, - IAppManager $appManager, - ITempManager $tempManager, - ICacheFactory $cachFactory, - ILogger $logger) { - $this->appData = $appData; - $this->appManager = $appManager; - $this->modelMapper = $modelMapper; - $this->tempManager = $tempManager; - $this->cacheFactory = $cachFactory; - $this->logger = $logger; + public function __construct( + private ModelMapper $modelMapper, + private IAppData $appData, + private IAppManager $appManager, + private ITempManager $tempManager, + private ICacheFactory $cacheFactory, + private LoggerInterface $logger, + ) { } /**