Skip to content

Commit ee4d196

Browse files
authored
Merge pull request #5 from OS2web/feature/adjust-watchdog-logger
ITKDev: Updated Watchdog logger
2 parents aac18a8 + 7bd7bb5 commit ee4d196

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
- Made Watchdog default logger
12+
- Updated Watchlog logger
13+
1114
## [0.1.0] - 2024-10-21
1215

1316
- Moved drush services into own yml file (drush 12)
@@ -28,6 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2831

2932
[Unreleased]: https://github.com/OS2web/os2web_audit/compare/develop...HEAD
3033
[0.1.0]: https://github.com/OS2web/os2web_audit/compare/0.0.3...0.1.0
31-
[0.0.2]: https://github.com/OS2web/os2web_audit/compare/0.0.2...0.0.3
34+
[0.0.3]: https://github.com/OS2web/os2web_audit/compare/0.0.2...0.0.3
3235
[0.0.2]: https://github.com/OS2web/os2web_audit/compare/0.0.1...0.0.2
3336
[0.0.1]: https://github.com/OS2web/os2web_audit/releases/tag/0.0.1

src/Form/SettingsForm.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* This is the settings for the module.
1515
*/
1616
class SettingsForm extends ConfigFormBase {
17+
public const OS2WEB_AUDIT_DEFUALT_PROVIDER = 'watchdog';
1718

1819
/**
1920
* {@inheritdoc}
@@ -75,7 +76,8 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
7576
'#title' => $this->t('Log provider'),
7677
'#description' => $this->t('Select the logger provider you which to use'),
7778
'#options' => $options,
78-
'#default_value' => $config->get('provider'),
79+
// We let watchdog be the default provider.
80+
'#default_value' => $config->get('provider') ?? self::OS2WEB_AUDIT_DEFUALT_PROVIDER,
7981
];
8082

8183
return parent::buildForm($form, $form_state);

src/Plugin/AuditLogger/Watchdog.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Drupal\os2web_audit\Plugin\AuditLogger;
44

55
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
6+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
67
use Drupal\Core\Plugin\PluginBase;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
79

810
/**
911
* Stores entities in the database.
@@ -14,7 +16,7 @@
1416
* description = @Translation("Store entity data in the database.")
1517
* )
1618
*/
17-
class Watchdog extends PluginBase implements AuditLoggerInterface {
19+
class Watchdog extends PluginBase implements AuditLoggerInterface, ContainerFactoryPluginInterface {
1820

1921
public function __construct(
2022
array $configuration,
@@ -25,6 +27,20 @@ public function __construct(
2527
parent::__construct($configuration, $plugin_id, $plugin_definition);
2628
}
2729

30+
/**
31+
* {@inheritdoc}
32+
*
33+
* @phpstan-param array<string, mixed> $configuration
34+
*/
35+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
36+
return new static(
37+
$configuration,
38+
$plugin_id,
39+
$plugin_definition,
40+
$container->get('logger.factory'),
41+
);
42+
}
43+
2844
/**
2945
* {@inheritdoc}
3046
*/
@@ -35,9 +51,9 @@ public function log(string $type, int $timestamp, string $message, array $metada
3551
});
3652

3753
$this->logger->get('os2web_audit')->info('%type: %line (%data)', [
38-
'type' => $type,
39-
'line' => $message,
40-
'data' => $data,
54+
'%type' => $type,
55+
'%line' => $message,
56+
'%data' => $data,
4157
]);
4258
}
4359

src/Service/Logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function error(string $type, string $line, bool $logUser = TRUE, array $m
7878
*/
7979
private function log(string $type, int $timestamp, string $line, bool $logUser = FALSE, array $metadata = []): void {
8080
$config = $this->configFactory->get(SettingsForm::$configName);
81-
$plugin_id = $config->get('provider');
81+
$plugin_id = $config->get('provider') ?? SettingsForm::OS2WEB_AUDIT_DEFUALT_PROVIDER;
8282
$configuration = $this->configFactory->get(PluginSettingsForm::getConfigName())->get($plugin_id);
8383

8484
if ($logUser) {

0 commit comments

Comments
 (0)