diff --git a/os2web_audit.info.yml b/os2web_audit.info.yml index b0c7153..0c06595 100644 --- a/os2web_audit.info.yml +++ b/os2web_audit.info.yml @@ -1,4 +1,4 @@ name: OS2web Audit type: module -description: 'OS2web Audit Module (log all events to external service)' +description: 'OS2web Audit Module (log events in the system)' core_version_requirement: ^8 || ^9 || ^10 diff --git a/os2web_audit.links.menu.yml b/os2web_audit.links.menu.yml index 574ad72..269a099 100644 --- a/os2web_audit.links.menu.yml +++ b/os2web_audit.links.menu.yml @@ -1,5 +1,5 @@ os2web_audit.admin_settings: - title: 'OS2web Audit settings' + title: 'OS2 Audit settings' parent: system.admin_config_system - description: 'Settings for the OS2web Audit module' + description: 'Settings for the OS2 Audit module' route_name: os2web_audit.plugin_settings_local_tasks diff --git a/src/Service/Logger.php b/src/Service/Logger.php index c1a6078..1d28fb4 100644 --- a/src/Service/Logger.php +++ b/src/Service/Logger.php @@ -20,6 +20,48 @@ public function __construct( ) { } + /** + * Logs a message at info level. + * + * @param string $type + * The type of event to log (auth, lookup etc.) + * @param int $timestamp + * The timestamp for the log message. + * @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 message. Default is an empty array. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ + public function info(string $type, int $timestamp, string $line, bool $logUser = false, array $metadata = []): void { + $this->log($type, $timestamp, $line, $logUser, $metadata + ['level' => 'info']); + } + + /** + * Logs a message at error level. + * + * @param string $type + * The type of event to log (auth, lookup etc.) + * @param int $timestamp + * The timestamp for the log message. + * @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 message. Default is an empty array. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + */ + public function error(string $type, int $timestamp, string $line, bool $logUser = false, array $metadata = []): void { + $this->log($type, $timestamp, $line, $logUser, $metadata + ['level' => 'error']); + } + /** * Logs a message using a plugin-specific logger. * @@ -29,12 +71,15 @@ public function __construct( * The timestamp for the log message. * @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 message. Default is an empty array. * * @throws \Drupal\Component\Plugin\Exception\PluginException */ - public function log(string $type, int $timestamp, string $line, array $metadata = []): void { + 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'); @@ -43,6 +88,12 @@ public function log(string $type, int $timestamp, string $line, array $metadata $configuration = $this->configFactory->get(PluginSettingsForm::getConfigName())->get($plugin_id); $logger = $this->loggerManager->createInstance($plugin_id, $configuration ?? []); + if ($logUser) { + // Add user id to the log message metadata. + $user = \Drupal::currentUser(); + $metadata['userId'] = $user->id(); + } + $logger->log($type, $timestamp, $line, $metadata); }