Skip to content

Commit

Permalink
ITKDev: Added user id to logging helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Aug 21, 2024
1 parent c60de69 commit f7ff39e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function getFileContent(array $element, WebformSubmissionInterface
/** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager $os2web_datalookup_plugins */
$os2web_datalookup_plugins = \Drupal::service('plugin.manager.os2web_datalookup');

/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupCPRInterface $cprPlugin */
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterfaceCpr $cprPlugin */
$cprPlugin = $os2web_datalookup_plugins->createDefaultInstanceByGroup('cpr_lookup');

if (!empty($nemid_cpr) && $cprPlugin->isReady()) {
Expand Down
20 changes: 16 additions & 4 deletions os2forms.module
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,27 @@ function os2forms_module_implements_alter(&$implementations, $hook) {
* The timestamp of the log entry.
* @param string $line
* The log message.
* @param bool $logUser
* Log information about the current logged-in user (need to track who has
* lookup information in external services). Default: false.
* @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 {
function os2forms_audit_log(string $type, int $timestamp, string $line, bool $logUser = false, array $metadata = []): void {
if ( \Drupal::service('module_handler')->moduleExists('os2form_audit')) {
if ($logUser) {
// Add user id to the log message.
$user = \Drupal::currentUser();
$metadata['userId'] = $user->id();
}

/** @var \Drupal\os2forms_audit\Service\Logger $logger */
$logger = \Drupal::service('os2form_audit.logger');
$logger->log($type, $timestamp, $line, $metadata);
try {
$logger->log($type, $timestamp, $line, $metadata);
}
catch (\Drupal\Component\Plugin\Exception\PluginException $e) {
// @todo: what should we do when plugins fails.
}
}
}

0 comments on commit f7ff39e

Please sign in to comment.