Skip to content

Commit

Permalink
ITKDev: Added better logging options
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Apr 25, 2024
1 parent 488a30a commit b061683
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion os2web_audit.info.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions os2web_audit.links.menu.yml
Original file line number Diff line number Diff line change
@@ -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
53 changes: 52 additions & 1 deletion src/Service/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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');

Expand All @@ -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);
}

Expand Down

0 comments on commit b061683

Please sign in to comment.