From 03d7800aff69abbe2d0963c0d89fc438cd10960e Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Tue, 19 Nov 2024 14:37:48 +0100 Subject: [PATCH 1/2] ITKDev: Updated Watchdog logger --- CHANGELOG.md | 3 +++ src/Form/SettingsForm.php | 4 +++- src/Plugin/AuditLogger/Watchdog.php | 22 ++++++++++++++++++---- src/Service/Logger.php | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54da9b9..8eb1d18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Made Watchdog default logger +- Updated Watchlog logger + ## [0.1.0] - 2024-10-21 - Moved drush services into own yml file (drush 12) diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index ad9ccd7..cf2c471 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -14,6 +14,7 @@ * This is the settings for the module. */ class SettingsForm extends ConfigFormBase { + public const OS2WEB_AUDIT_DEFUALT_PROVIDER = 'watchdog'; /** * {@inheritdoc} @@ -75,7 +76,8 @@ public function buildForm(array $form, FormStateInterface $form_state): array { '#title' => $this->t('Log provider'), '#description' => $this->t('Select the logger provider you which to use'), '#options' => $options, - '#default_value' => $config->get('provider'), + // We let watchdog be the default provider. + '#default_value' => $config->get('provider') ?? self::OS2WEB_AUDIT_DEFUALT_PROVIDER, ]; return parent::buildForm($form, $form_state); diff --git a/src/Plugin/AuditLogger/Watchdog.php b/src/Plugin/AuditLogger/Watchdog.php index b368bb9..b75d93d 100644 --- a/src/Plugin/AuditLogger/Watchdog.php +++ b/src/Plugin/AuditLogger/Watchdog.php @@ -3,7 +3,9 @@ namespace Drupal\os2web_audit\Plugin\AuditLogger; use Drupal\Core\Logger\LoggerChannelFactoryInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Stores entities in the database. @@ -14,7 +16,7 @@ * description = @Translation("Store entity data in the database.") * ) */ -class Watchdog extends PluginBase implements AuditLoggerInterface { +class Watchdog extends PluginBase implements AuditLoggerInterface, ContainerFactoryPluginInterface { public function __construct( array $configuration, @@ -25,6 +27,18 @@ public function __construct( parent::__construct($configuration, $plugin_id, $plugin_definition); } + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('logger.factory'), + ); + } + /** * {@inheritdoc} */ @@ -35,9 +49,9 @@ public function log(string $type, int $timestamp, string $message, array $metada }); $this->logger->get('os2web_audit')->info('%type: %line (%data)', [ - 'type' => $type, - 'line' => $message, - 'data' => $data, + '%type' => $type, + '%line' => $message, + '%data' => $data, ]); } diff --git a/src/Service/Logger.php b/src/Service/Logger.php index 8101a05..ab07551 100644 --- a/src/Service/Logger.php +++ b/src/Service/Logger.php @@ -78,7 +78,7 @@ public function error(string $type, string $line, bool $logUser = TRUE, array $m */ private function log(string $type, int $timestamp, string $line, bool $logUser = FALSE, array $metadata = []): void { $config = $this->configFactory->get(SettingsForm::$configName); - $plugin_id = $config->get('provider'); + $plugin_id = $config->get('provider') ?? SettingsForm::OS2WEB_AUDIT_DEFUALT_PROVIDER; $configuration = $this->configFactory->get(PluginSettingsForm::getConfigName())->get($plugin_id); if ($logUser) { From 7bd7bb546a1425335d778d5a2e5858da09b4d1f8 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Tue, 19 Nov 2024 14:45:23 +0100 Subject: [PATCH 2/2] ITKDev: Coding standards --- CHANGELOG.md | 2 +- src/Plugin/AuditLogger/Watchdog.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eb1d18..43b03cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [Unreleased]: https://github.com/OS2web/os2web_audit/compare/develop...HEAD [0.1.0]: https://github.com/OS2web/os2web_audit/compare/0.0.3...0.1.0 -[0.0.2]: https://github.com/OS2web/os2web_audit/compare/0.0.2...0.0.3 +[0.0.3]: https://github.com/OS2web/os2web_audit/compare/0.0.2...0.0.3 [0.0.2]: https://github.com/OS2web/os2web_audit/compare/0.0.1...0.0.2 [0.0.1]: https://github.com/OS2web/os2web_audit/releases/tag/0.0.1 diff --git a/src/Plugin/AuditLogger/Watchdog.php b/src/Plugin/AuditLogger/Watchdog.php index b75d93d..f6cb637 100644 --- a/src/Plugin/AuditLogger/Watchdog.php +++ b/src/Plugin/AuditLogger/Watchdog.php @@ -29,6 +29,8 @@ public function __construct( /** * {@inheritdoc} + * + * @phpstan-param array $configuration */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static(