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

Deprecate LoggerTrait #4140

Merged
merged 13 commits into from
Mar 4, 2024
4 changes: 4 additions & 0 deletions modules/common/common.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ services:
- [setResourceMapper, ['@?dkan.metastore.resource_mapper']]
- [setImportInfo, ['@?dkan.datastore.import_info']]

dkan.common.logger_channel:
parent: logger.channel_base
arguments: [ 'dkan' ]

plugin.manager.dkan_api_docs:
class: \Drupal\common\Plugin\DkanApiDocsPluginManager
parent: default_plugin_manager
Expand Down
2 changes: 2 additions & 0 deletions modules/common/src/LoggerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* DKAN logger channel trait.
*
* @deprecated Use an injected logger service instead.
*/
trait LoggerTrait {

Expand Down
5 changes: 3 additions & 2 deletions modules/datastore/datastore.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ services:
- '@dkan.metastore.reference_lookup'
- '@dkan.metastore.storage'
- '@dkan.datastore.service'
calls:
- [setLoggerFactory, ['@logger.factory']]
- '@dkan.datastore.logger_channel'

dkan.datastore.service.factory.import:
class: \Drupal\datastore\Service\Factory\ImportServiceFactory
arguments:
- '@dkan.datastore.import_job_store_factory'
- '@dkan.datastore.database_table_factory'
- '@dkan.datastore.logger_channel'

dkan.datastore.logger_channel:
parent: logger.channel_base
Expand All @@ -81,6 +81,7 @@ services:
class: \Drupal\datastore\Storage\DatabaseTableFactory
arguments:
- '@dkan.datastore.database'
- '@dkan.datastore.logger_channel'

dkan.datastore.sql_endpoint.service:
class: \Drupal\datastore\SqlEndpoint\DatastoreSqlEndpointService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
services:
dkan.datastore.service.factory.import:
dkan.datastore_mysql_import.service.factory.import:
decorates: dkan.datastore.service.factory.import
class: \Drupal\datastore_mysql_import\Factory\MysqlImportFactory
arguments:
- '@dkan.datastore.import_job_store_factory'
- '@dkan.datastore_mysql_import.database_table_factory'
- '@dkan.datastore.logger_channel'

dkan.datastore_mysql_import.database_table_factory:
class: \Drupal\datastore_mysql_import\Storage\MySqlDatabaseTableFactory
arguments:
- '@dkan.datastore.database'
- '@dkan.datastore.logger_channel'
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\datastore\Storage\ImportJobStoreFactory;
use Drupal\datastore_mysql_import\Service\MysqlImport;
use Drupal\datastore_mysql_import\Storage\MySqlDatabaseTableFactory;
use Psr\Log\LoggerInterface;

/**
* Mysql importer factory.
Expand All @@ -27,12 +28,24 @@ class MysqlImportFactory implements ImportFactoryInterface {
*/
private $databaseTableFactory;

/**
* DKAN logger channel service.
*
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;

/**
* Constructor.
*/
public function __construct(ImportJobStoreFactory $importJobStoreFactory, MySqlDatabaseTableFactory $databaseTableFactory) {
public function __construct(
ImportJobStoreFactory $importJobStoreFactory,
MySqlDatabaseTableFactory $databaseTableFactory,
LoggerInterface $loggerChannel
) {
$this->importJobStoreFactory = $importJobStoreFactory;
$this->databaseTableFactory = $databaseTableFactory;
$this->logger = $loggerChannel;
}

/**
Expand All @@ -46,7 +59,12 @@ public function getInstance(string $identifier, array $config = []) {
throw new \Exception("config['resource'] is required");
}

$importer = new ImportService($resource, $this->importJobStoreFactory, $this->databaseTableFactory);
$importer = new ImportService(
$resource,
$this->importJobStoreFactory,
$this->databaseTableFactory,
$this->logger
);
$importer->setImporterClass(MysqlImport::class);
return $importer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MySqlDatabaseTableFactory extends DatabaseTableFactory {
* {@inheritDoc}
*/
protected function getDatabaseTable($resource) {
return new MySqlDatabaseTable($this->connection, $resource);
return new MySqlDatabaseTable($this->connection, $resource, $this->logger);
}

}
22 changes: 20 additions & 2 deletions modules/datastore/src/Service/Factory/ImportServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\datastore\Service\ImportService;
use Drupal\datastore\Storage\DatabaseTableFactory;
use Drupal\datastore\Storage\ImportJobStoreFactory;
use Psr\Log\LoggerInterface;

/**
* Create an importer object for a given resource.
Expand All @@ -25,12 +26,24 @@ class ImportServiceFactory implements ImportFactoryInterface {
*/
private $databaseTableFactory;

/**
* DKAN logger channel service.
*
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;

/**
* Constructor.
*/
public function __construct(ImportJobStoreFactory $importJobStoreFactory, DatabaseTableFactory $databaseTableFactory) {
public function __construct(
ImportJobStoreFactory $importJobStoreFactory,
DatabaseTableFactory $databaseTableFactory,
LoggerInterface $loggerChannel
) {
$this->importJobStoreFactory = $importJobStoreFactory;
$this->databaseTableFactory = $databaseTableFactory;
$this->logger = $loggerChannel;
}

/**
Expand All @@ -40,7 +53,12 @@ public function __construct(ImportJobStoreFactory $importJobStoreFactory, Databa
*/
public function getInstance(string $identifier, array $config = []) {
if ($resource = $config['resource'] ?? FALSE) {
return new ImportService($resource, $this->importJobStoreFactory, $this->databaseTableFactory);
return new ImportService(
$resource,
$this->importJobStoreFactory,
$this->databaseTableFactory,
$this->logger
);
}
throw new \Exception("config['resource'] is required");
}
Expand Down
28 changes: 21 additions & 7 deletions modules/datastore/src/Service/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Drupal\datastore\Service;

use CsvParser\Parser\Csv;
use Drupal\datastore\Plugin\QueueWorker\ImportJob;
use Drupal\common\EventDispatcherTrait;
use Drupal\common\LoggerTrait;
use Drupal\common\DataResource;
use Drupal\common\EventDispatcherTrait;
use Drupal\datastore\Plugin\QueueWorker\ImportJob;
use Drupal\datastore\Storage\DatabaseTable;
use Drupal\datastore\Storage\DatabaseTableFactory;
use Drupal\datastore\Storage\ImportJobStoreFactory;
use Procrastinator\Result;
use Psr\Log\LoggerInterface;

/**
* Datastore importer.
Expand All @@ -20,7 +20,7 @@
* as a property.
*/
class ImportService {
use LoggerTrait;

use EventDispatcherTrait;

/**
Expand Down Expand Up @@ -76,6 +76,13 @@ class ImportService {
*/
private ?ImportJob $importJob = NULL;

/**
* Logger channel service.
*
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;

/**
* Create a resource service instance.
*
Expand All @@ -85,11 +92,19 @@ class ImportService {
* Import jobstore factory.
* @param \Drupal\datastore\Storage\DatabaseTableFactory $databaseTableFactory
* Database Table factory.
* @param \Psr\Log\LoggerInterface $loggerChannel
* DKAN logger channel service.
*/
public function __construct(DataResource $resource, ImportJobStoreFactory $importJobStoreFactory, DatabaseTableFactory $databaseTableFactory) {
public function __construct(
DataResource $resource,
ImportJobStoreFactory $importJobStoreFactory,
DatabaseTableFactory $databaseTableFactory,
LoggerInterface $loggerChannel
) {
$this->resource = $resource;
$this->importJobStoreFactory = $importJobStoreFactory;
$this->databaseTableFactory = $databaseTableFactory;
$this->logger = $loggerChannel;
}

/**
Expand Down Expand Up @@ -117,8 +132,7 @@ public function import() {

if ($result->getStatus() === Result::ERROR) {
$datastore_resource = $this->getResource()->getDatastoreResource();
$this->setLoggerFactory(\Drupal::service('logger.factory'));
$this->error('Error importing resource id:%id path:%path message:%message', [
$this->logger->error('Error importing resource id:%id path:%path message:%message', [
'%id' => $datastore_resource->getId(),
'%path' => $datastore_resource->getFilePath(),
'%message' => $result->getError(),
Expand Down
2 changes: 0 additions & 2 deletions modules/datastore/src/Service/ResourceLocalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Contracts\FactoryInterface;
use Drupal\common\DataResource;
use Drupal\common\EventDispatcherTrait;
use Drupal\common\LoggerTrait;
use Drupal\common\UrlHostTokenResolver;
use Drupal\common\Util\DrupalFiles;
use Drupal\Core\File\FileSystemInterface;
Expand All @@ -21,7 +20,6 @@
*/
class ResourceLocalizer {

use LoggerTrait;
use EventDispatcherTrait;

/**
Expand Down
36 changes: 25 additions & 11 deletions modules/datastore/src/Service/ResourcePurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;

use Drupal\common\LoggerTrait;
use Drupal\common\DataResource;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\common\DataResource;
use Drupal\datastore\DatastoreService;
use Drupal\metastore\ReferenceLookupInterface;
use Drupal\metastore\Storage\DataFactory;
use Drupal\node\NodeInterface;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Resource purger service.
*/
class ResourcePurger implements ContainerInjectionInterface {
use LoggerTrait;

/**
* The datastore.settings config.
Expand Down Expand Up @@ -49,6 +46,13 @@ class ResourcePurger implements ContainerInjectionInterface {
*/
private $storage;

/**
* DKAN logger channel service.
*
* @var \Psr\Log\LoggerInterface
*/
private LoggerInterface $logger;

/**
* Constructs a ResourcePurger object.
*
Expand All @@ -60,12 +64,21 @@ class ResourcePurger implements ContainerInjectionInterface {
* The dkan.metastore.storage service.
* @param \Drupal\datastore\DatastoreService $datastore
* The dkan.datastore.service service.
* @param \Psr\Log\LoggerInterface $loggerChannel
* DKAN logger channel service.
*/
public function __construct(ConfigFactoryInterface $configFactory, ReferenceLookupInterface $referenceLookup, DataFactory $dataFactory, DatastoreService $datastore) {
public function __construct(
ConfigFactoryInterface $configFactory,
ReferenceLookupInterface $referenceLookup,
DataFactory $dataFactory,
DatastoreService $datastore,
LoggerInterface $loggerChannel
) {
$this->config = $configFactory->get('datastore.settings');
$this->referenceLookup = $referenceLookup;
$this->storage = $dataFactory->getInstance('dataset');
$this->datastore = $datastore;
$this->logger = $loggerChannel;
}

/**
Expand All @@ -76,7 +89,8 @@ public static function create(ContainerInterface $container) {
$container->get('config.factory'),
$container->get('dkan.metastore.reference_lookup'),
$container->get('dkan.metastore.storage'),
$container->get('dkan.datastore.service')
$container->get('dkan.datastore.service'),
$container->get('dkan.datastore.logger_channel')
);
}

Expand Down Expand Up @@ -147,7 +161,7 @@ private function queue(array $uuids, bool $prior) {
'uuids' => $uuids,
'prior' => $prior,
]);
$this->notice('Queued resource purging with queueId:%queueId uuids:%uuids', [
$this->logger->notice('Queued resource purging with queueId:%queueId uuids:%uuids', [
'%queueId' => $queueId,
'%uuids' => implode(', ', $uuids),
]);
Expand Down Expand Up @@ -184,7 +198,7 @@ public function purgeHelper(int $vid, string $uuid, bool $prior) {
$this->purge($vid, $uuid, $prior);
}
catch (\Exception $e) {
$this->error("Error purging uuid {$uuid}, revision id {$vid}: " . $e->getMessage());
$this->logger->error("Error purging uuid {$uuid}, revision id {$vid}: " . $e->getMessage());
}
}

Expand Down Expand Up @@ -387,7 +401,7 @@ private function removeResourceLocalizer(string $id, string $version) {
$this->datastore->getResourceLocalizer()->remove($id, $version);
}
catch (\Exception $e) {
$this->error("Error removing resource localizer id {$id}, version {$version}: " . $e->getMessage());
$this->logger->error("Error removing resource localizer id {$id}, version {$version}: " . $e->getMessage());
}
}

Expand All @@ -404,7 +418,7 @@ private function removeDatastoreStorage(string $id, string $version) {
$this->datastore->getStorage($id, $version)->destruct();
}
catch (\Exception $e) {
$this->error("Error deleting datastore id {$id}, version {$version}: " . $e->getMessage());
$this->logger->error("Error deleting datastore id {$id}, version {$version}: " . $e->getMessage());
}
}

Expand Down
Loading