Skip to content

Commit 1bf3fcb

Browse files
committed
Reduced logging significantly
1 parent 8d339b9 commit 1bf3fcb

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

config/install/advancedqueue.advancedqueue_queue.os2web_audit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ label: os2web_audit
55
backend: database
66
backend_configuration:
77
lease_time: 300
8-
processor: cron
8+
processor: daemon
99
processing_time: 90
1010
locked: false

src/Plugin/AdvancedQueue/JobType/LogMessages.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Drupal\os2web_audit\Plugin\AdvancedQueue\JobType;
44

5-
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
5+
use Drupal\Component\Plugin\Exception\PluginException;
66
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
77
use Drupal\advancedqueue\Job;
88
use Drupal\advancedqueue\JobResult;
99
use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase;
10+
use Drupal\os2web_audit\Exception\AuditException;
11+
use Drupal\os2web_audit\Exception\ConnectionException;
1012
use Drupal\os2web_audit\Service\Logger;
1113
use Symfony\Component\DependencyInjection\ContainerInterface;
1214

@@ -31,7 +33,6 @@ public static function create(ContainerInterface $container, array $configuratio
3133
$plugin_id,
3234
$plugin_definition,
3335
$container->get('os2web_audit.logger'),
34-
$container->get('logger.factory'),
3536
);
3637
}
3738

@@ -45,7 +46,6 @@ public function __construct(
4546
$plugin_id,
4647
$plugin_definition,
4748
private readonly Logger $logger,
48-
private readonly LoggerChannelFactoryInterface $watchdog,
4949
) {
5050
parent::__construct($configuration, $plugin_id, $plugin_definition);
5151
}
@@ -56,20 +56,12 @@ public function __construct(
5656
public function process(Job $job): JobResult {
5757
$payload = $job->getPayload();
5858

59-
$logger_context = [
60-
'job_id' => $job->getId(),
61-
'operation' => 'response from queue',
62-
];
63-
6459
try {
6560
$this->logger->log($payload['type'], $payload['timestamp'], $payload['line'], $payload['plugin_id'], $payload['metadata']);
66-
$this->watchdog->get(Logger::OS2WEB_AUDIT_LOGGER_CHANNEL)->info('Successfully audit logged message.', $logger_context);
6761

6862
return JobResult::success();
6963
}
70-
catch (\Exception $e) {
71-
$this->watchdog->get(Logger::OS2WEB_AUDIT_LOGGER_CHANNEL)->error(sprintf('Failed audit logging message: %s', $e->getMessage()), $logger_context);
72-
64+
catch (PluginException | ConnectionException | AuditException $e) {
7365
return JobResult::failure($e->getMessage());
7466
}
7567
}

src/Service/Logger.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,27 @@ private function createLoggingJob(string $type, int $timestamp, string $line, bo
100100
$config = $this->configFactory->get(SettingsForm::$configName);
101101
$plugin_id = $config->get('provider') ?? SettingsForm::OS2WEB_AUDIT_DEFUALT_PROVIDER;
102102

103-
$queueStorage = $this->entityTypeManager->getStorage('advancedqueue_queue');
104-
/** @var \Drupal\advancedqueue\Entity\Queue $queue */
105-
$queue = $queueStorage->load(self::OS2WEB_AUDIT_QUEUE_ID);
106-
$job = Job::create(LogMessages::class, [
103+
$payload = [
107104
'type' => $type,
108105
'timestamp' => $timestamp,
109106
'line' => $line,
110107
'plugin_id' => $plugin_id,
111108
'metadata' => $metadata,
112-
]);
109+
];
113110

114-
$queue->enqueueJob($job);
111+
try {
112+
$queueStorage = $this->entityTypeManager->getStorage('advancedqueue_queue');
113+
/** @var \Drupal\advancedqueue\Entity\Queue $queue */
114+
$queue = $queueStorage->load(self::OS2WEB_AUDIT_QUEUE_ID);
115115

116-
$logger_context = [
117-
'job_id' => $job->getId(),
118-
'operation' => 'log message queued',
119-
];
116+
$job = Job::create(LogMessages::class, $payload);
117+
118+
$queue->enqueueJob($job);
119+
}
120+
catch (\Exception $exception) {
121+
$this->watchdog->get(self::OS2WEB_AUDIT_LOGGER_CHANNEL)->error(sprintf('Failed creating job: %s', $exception->getMessage()), $payload);
122+
}
120123

121-
$this->watchdog->get(self::OS2WEB_AUDIT_LOGGER_CHANNEL)->notice('Added audit logging message to queue for processing.', $logger_context);
122124
}
123125

124126
/**
@@ -135,22 +137,17 @@ private function createLoggingJob(string $type, int $timestamp, string $line, bo
135137
* @param array<string, string> $metadata
136138
* Additional metadata for the log message. Default is an empty array.
137139
*
138-
* @throws \Exception.
140+
* @throws \Drupal\Component\Plugin\Exception\PluginException
141+
* @throws \Drupal\os2web_audit\Exception\ConnectionException
142+
* @throws \Drupal\os2web_audit\Exception\AuditException
139143
*/
140144
public function log(string $type, int $timestamp, string $line, string $plugin_id, array $metadata = []): void {
141145

142146
$configuration = $this->configFactory->get(PluginSettingsForm::getConfigName())->get($plugin_id);
143147

144-
try {
145-
/** @var \Drupal\os2web_audit\Plugin\AuditLogger\AuditLoggerInterface $logger */
146-
$logger = $this->loggerManager->createInstance($plugin_id, $configuration ?? []);
147-
$logger->log($type, $timestamp, $line, $metadata);
148-
}
149-
catch (\Exception $e) {
150-
// Log (not audit log) an error and throw error in order to retry.
151-
$this->watchdog->get(self::OS2WEB_AUDIT_LOGGER_CHANNEL)->error($e->getMessage());
152-
throw $e;
153-
}
148+
/** @var \Drupal\os2web_audit\Plugin\AuditLogger\AuditLoggerInterface $logger */
149+
$logger = $this->loggerManager->createInstance($plugin_id, $configuration ?? []);
150+
$logger->log($type, $timestamp, $line, $metadata);
154151
}
155152

156153
}

0 commit comments

Comments
 (0)