Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace deprecated ILogger with Psr\Log\LoggerInterface #931

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions lib/BackgroundJob/TrainJobIpV4.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
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);
Expand All @@ -38,8 +34,6 @@ public function __construct(TrainService $trainService,
if (defined('\OCP\BackgroundJob\IJob::TIME_INSENSITIVE') && method_exists($this, 'setTimeSensitivity')) {
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}
$this->trainService = $trainService;
$this->logger = $logger;
}

/**
Expand All @@ -56,15 +50,9 @@ protected function run($argument) {
$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]);
}
}
}
28 changes: 8 additions & 20 deletions lib/BackgroundJob/TrainJobIpV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
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);
Expand All @@ -38,8 +34,6 @@ public function __construct(TrainService $trainService,
if (defined('\OCP\BackgroundJob\IJob::TIME_INSENSITIVE') && method_exists($this, 'setTimeSensitivity')) {
$this->setTimeSensitivity(self::TIME_INSENSITIVE);
}
$this->trainService = $trainService;
$this->logger = $logger;
}

/**
Expand All @@ -56,15 +50,9 @@ protected function run($argument) {
$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]);
}
}
}
35 changes: 9 additions & 26 deletions lib/Listener/LoginMailListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,24 @@
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<SuspiciousLoginEvent>
*/
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 {
Expand All @@ -69,10 +55,7 @@ public function handle(Event $event): void {
$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]);
}
}

Expand Down
23 changes: 7 additions & 16 deletions lib/Listener/LoginNotificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@
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<SuspiciousLoginEvent>
*/
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 {
Expand All @@ -52,8 +44,7 @@ public function handle(Event $event): void {
]);
$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]);
}
}
}
22 changes: 6 additions & 16 deletions lib/Service/ETLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,18 @@
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 {
Expand Down
16 changes: 5 additions & 11 deletions lib/Service/EstimatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@
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,
) {
}

/**
Expand Down
43 changes: 10 additions & 33 deletions lib/Service/LoginClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
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;
Expand All @@ -27,36 +27,14 @@

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,
) {
}

/**
Expand Down Expand Up @@ -126,8 +104,7 @@ private function persistSuspiciousLogin(string $uid, string $ip): ?SuspiciousLog

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;
}
}
Expand Down
40 changes: 9 additions & 31 deletions lib/Service/ModelStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
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;
Expand All @@ -32,36 +32,14 @@
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,
) {
}

/**
Expand Down
Loading
Loading