From c60de6903ac086da3393848987c82d76f55106e6 Mon Sep 17 00:00:00 2001 From: Jesper Kristensen Date: Wed, 24 Apr 2024 11:15:55 +0200 Subject: [PATCH] ITKDev: Added wrapper function for audit logging --- .../src/Commands/AuditLogDrushCommands.php | 3 +++ .../src/Form/PluginSettingsForm.php | 2 +- .../os2forms_audit/src/Form/SettingsForm.php | 2 +- modules/os2forms_audit/src/Service/Logger.php | 2 ++ os2forms.module | 25 +++++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/modules/os2forms_audit/src/Commands/AuditLogDrushCommands.php b/modules/os2forms_audit/src/Commands/AuditLogDrushCommands.php index b6af8064..3ba4769c 100644 --- a/modules/os2forms_audit/src/Commands/AuditLogDrushCommands.php +++ b/modules/os2forms_audit/src/Commands/AuditLogDrushCommands.php @@ -35,6 +35,9 @@ public function __construct( * @throws \Drupal\Component\Plugin\Exception\PluginException */ public function logMessage(string $log_message = ''): void { + if (empty($log_message)) { + throw new \Exception('Log message cannot be empty.'); + } $this->auditLogger->log('test', time(), $log_message, ['from' => 'drush']); } diff --git a/modules/os2forms_audit/src/Form/PluginSettingsForm.php b/modules/os2forms_audit/src/Form/PluginSettingsForm.php index f2e535e7..ea88d066 100644 --- a/modules/os2forms_audit/src/Form/PluginSettingsForm.php +++ b/modules/os2forms_audit/src/Form/PluginSettingsForm.php @@ -40,7 +40,7 @@ public function __construct( /** * {@inheritdoc} */ - public static function create(ContainerInterface $container): PluginSettingsForm|ConfigFormBase|static { + public static function create(ContainerInterface $container): static { return new static( $container->get('config.factory'), $container->get('plugin.manager.os2forms_audit_logger') diff --git a/modules/os2forms_audit/src/Form/SettingsForm.php b/modules/os2forms_audit/src/Form/SettingsForm.php index 7c7964ac..9e3e68e2 100644 --- a/modules/os2forms_audit/src/Form/SettingsForm.php +++ b/modules/os2forms_audit/src/Form/SettingsForm.php @@ -28,7 +28,7 @@ public function __construct( /** * {@inheritdoc} */ - public static function create(ContainerInterface $container) { + public static function create(ContainerInterface $container): static { return new static( $container->get('config.factory'), $container->get('plugin.manager.os2forms_audit_logger') diff --git a/modules/os2forms_audit/src/Service/Logger.php b/modules/os2forms_audit/src/Service/Logger.php index ca0675e1..a11ab72d 100644 --- a/modules/os2forms_audit/src/Service/Logger.php +++ b/modules/os2forms_audit/src/Service/Logger.php @@ -38,6 +38,8 @@ public function log(string $type, int $timestamp, string $line, array $metadata $config = $this->configFactory->get(SettingsForm::$configName); $plugin_id = $config->get('provider'); + // @todo: default logger (file) + // @todo: Fallback logger on error. $configuration = $this->configFactory->get(PluginSettingsForm::getConfigName())->get($plugin_id); $logger = $this->loggerManager->createInstance($plugin_id, $configuration ?? []); diff --git a/os2forms.module b/os2forms.module index 1357b459..8f22ac25 100644 --- a/os2forms.module +++ b/os2forms.module @@ -357,3 +357,28 @@ function os2forms_module_implements_alter(&$implementations, $hook) { break; } } + +/** + * Logging function for audit logs. + * + * Wrapper to make logging easier and simplify the usage of the audit logger, if + * enabled. + * + * @param string $type + * The type of log entry. + * @param int $timestamp + * The timestamp of the log entry. + * @param string $line + * The log message. + * @param array $metadata + * Additional metadata for the log entry. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ +function os2forms_audit_log(string $type, int $timestamp, string $line, array $metadata = []): void { + if ( \Drupal::service('module_handler')->moduleExists('os2form_audit')) { + /** @var \Drupal\os2forms_audit\Service\Logger $logger */ + $logger = \Drupal::service('os2form_audit.logger'); + $logger->log($type, $timestamp, $line, $metadata); + } +}