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

ITKDev: Updated Watchdog logger #5

Merged
merged 2 commits into from
Nov 19, 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,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
4 changes: 3 additions & 1 deletion src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* This is the settings for the module.
*/
class SettingsForm extends ConfigFormBase {
public const OS2WEB_AUDIT_DEFUALT_PROVIDER = 'watchdog';

/**
* {@inheritdoc}
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 20 additions & 4 deletions src/Plugin/AuditLogger/Watchdog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -25,6 +27,20 @@ public function __construct(
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $configuration
*/
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}
*/
Expand All @@ -35,9 +51,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,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Service/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading