Skip to content

Commit

Permalink
ITKDev: Added wrapper function for audit logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Aug 21, 2024
1 parent e327173 commit c60de69
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions modules/os2forms_audit/src/Commands/AuditLogDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
2 changes: 1 addition & 1 deletion modules/os2forms_audit/src/Form/PluginSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion modules/os2forms_audit/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 2 additions & 0 deletions modules/os2forms_audit/src/Service/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? []);

Expand Down
25 changes: 25 additions & 0 deletions os2forms.module
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit c60de69

Please sign in to comment.