From c1452aa6bd615a4f82b010f48e0a7e4ea3ac012c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 26 May 2023 18:57:42 +0200 Subject: [PATCH 1/2] emit an event when a message is logged Signed-off-by: Robin Appelman --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + .../EventDispatcher/EventDispatcher.php | 7 ++ lib/private/Log.php | 16 ++++ lib/private/Log/PsrLoggerAdapter.php | 5 ++ lib/public/Log/BeforeMessageLoggedEvent.php | 78 +++++++++++++++++++ 6 files changed, 108 insertions(+) create mode 100644 lib/public/Log/BeforeMessageLoggedEvent.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 145bd9b8ce94c..7041429569237 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -473,6 +473,7 @@ 'OCP\\Lock\\ManuallyLockedException' => $baseDir . '/lib/public/Lock/ManuallyLockedException.php', 'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php', 'OCP\\Log\\Audit\\CriticalActionPerformedEvent' => $baseDir . '/lib/public/Log/Audit/CriticalActionPerformedEvent.php', + 'OCP\\Log\\BeforeMessageLoggedEvent' => $baseDir . '/lib/public/Log/BeforeMessageLoggedEvent.php', 'OCP\\Log\\IDataLogger' => $baseDir . '/lib/public/Log/IDataLogger.php', 'OCP\\Log\\IFileBased' => $baseDir . '/lib/public/Log/IFileBased.php', 'OCP\\Log\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 8d05bda3ad4d7..6fa8f7b4a09fe 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -506,6 +506,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Lock\\ManuallyLockedException' => __DIR__ . '/../../..' . '/lib/public/Lock/ManuallyLockedException.php', 'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php', 'OCP\\Log\\Audit\\CriticalActionPerformedEvent' => __DIR__ . '/../../..' . '/lib/public/Log/Audit/CriticalActionPerformedEvent.php', + 'OCP\\Log\\BeforeMessageLoggedEvent' => __DIR__ . '/../../..' . '/lib/public/Log/BeforeMessageLoggedEvent.php', 'OCP\\Log\\IDataLogger' => __DIR__ . '/../../..' . '/lib/public/Log/IDataLogger.php', 'OCP\\Log\\IFileBased' => __DIR__ . '/../../..' . '/lib/public/Log/IFileBased.php', 'OCP\\Log\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php', diff --git a/lib/private/EventDispatcher/EventDispatcher.php b/lib/private/EventDispatcher/EventDispatcher.php index 2a3063449233a..d64ad88be7e7c 100644 --- a/lib/private/EventDispatcher/EventDispatcher.php +++ b/lib/private/EventDispatcher/EventDispatcher.php @@ -27,6 +27,7 @@ */ namespace OC\EventDispatcher; +use OC\Log; use Psr\Log\LoggerInterface; use function get_class; use OC\Broadcast\Events\BroadcastEvent; @@ -54,6 +55,12 @@ public function __construct(SymfonyDispatcher $dispatcher, $this->dispatcher = $dispatcher; $this->container = $container; $this->logger = $logger; + + // inject the event dispatcher into the logger + // this is done here because there is a cyclic dependency between the event dispatcher and logger + if ($this->logger instanceof Log or $this->logger instanceof Log\PsrLoggerAdapter) { + $this->logger->setEventDispatcher($this); + } } public function addListener(string $eventName, diff --git a/lib/private/Log.php b/lib/private/Log.php index 5c1862370fed8..3ba87526941bc 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -38,6 +38,8 @@ use Exception; use Nextcloud\LogNormalizer\Normalizer; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Log\BeforeMessageLoggedEvent; use OCP\ILogger; use OCP\IUserSession; use OCP\Log\IDataLogger; @@ -65,6 +67,7 @@ class Log implements ILogger, IDataLogger { private ?bool $logConditionSatisfied = null; private ?Normalizer $normalizer; private ?IRegistry $crashReporters; + private ?IEventDispatcher $eventDispatcher; /** * @param IWriter $logger The logger that should be used @@ -91,6 +94,11 @@ public function __construct( $this->normalizer = $normalizer; } $this->crashReporters = $registry; + $this->eventDispatcher = null; + } + + public function setEventDispatcher(IEventDispatcher $eventDispatcher) { + $this->eventDispatcher = $eventDispatcher; } /** @@ -209,6 +217,10 @@ public function log(int $level, string $message, array $context = []) { $app = $context['app'] ?? 'no app in context'; $entry = $this->interpolateMessage($context, $message); + if ($this->eventDispatcher) { + $this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry)); + } + try { if ($level >= $minLevel) { $this->writeLog($app, $entry, $level); @@ -331,6 +343,10 @@ public function logException(Throwable $exception, array $context = []) { array_walk($context, [$this->normalizer, 'format']); + if ($this->eventDispatcher) { + $this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $data)); + } + try { if ($level >= $minLevel) { if (!$this->logger instanceof IFileBased) { diff --git a/lib/private/Log/PsrLoggerAdapter.php b/lib/private/Log/PsrLoggerAdapter.php index 80c4c187b13b1..07a898e2528b6 100644 --- a/lib/private/Log/PsrLoggerAdapter.php +++ b/lib/private/Log/PsrLoggerAdapter.php @@ -26,6 +26,7 @@ namespace OC\Log; use OC\Log; +use OCP\EventDispatcher\IEventDispatcher; use OCP\ILogger; use OCP\Log\IDataLogger; use Psr\Log\InvalidArgumentException; @@ -42,6 +43,10 @@ public function __construct(Log $logger) { $this->logger = $logger; } + public function setEventDispatcher(IEventDispatcher $eventDispatcher) { + $this->logger->setEventDispatcher($eventDispatcher); + } + private function containsThrowable(array $context): bool { return array_key_exists('exception', $context) && $context['exception'] instanceof Throwable; } diff --git a/lib/public/Log/BeforeMessageLoggedEvent.php b/lib/public/Log/BeforeMessageLoggedEvent.php new file mode 100644 index 0000000000000..42e57f0996489 --- /dev/null +++ b/lib/public/Log/BeforeMessageLoggedEvent.php @@ -0,0 +1,78 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Log; + +use OCP\EventDispatcher\Event; + +/** + * Even for when a log item is being logged + * + * @since 26.0.7 + */ +class BeforeMessageLoggedEvent extends Event { + private int $level; + private string $app; + private $message; + + /** + * @since 28.0.0 + */ + public function __construct(string $app, int $level, $message) { + $this->level = $level; + $this->app = $app; + $this->message = $message; + } + + /** + * Get the level of the log item + * + * @return int + * @since 28.0.0 + */ + public function getLevel(): int { + return $this->level; + } + + + /** + * Get the app context of the log item + * + * @return string + * @since 28.0.0 + */ + public function getApp(): string { + return $this->app; + } + + + /** + * Get the message of the log item + * + * @return string + * @since 28.0.0 + */ + public function getMessage(): string { + return $this->message; + } +} From d0fb60891d64de945e2189ac056e758f2ca8c133 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 11 Oct 2023 10:44:13 +0200 Subject: [PATCH 2/2] fix(api): set the correct version in since annotation Signed-off-by: Arthur Schiwon --- lib/public/Log/BeforeMessageLoggedEvent.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/public/Log/BeforeMessageLoggedEvent.php b/lib/public/Log/BeforeMessageLoggedEvent.php index 42e57f0996489..859ce6ec55035 100644 --- a/lib/public/Log/BeforeMessageLoggedEvent.php +++ b/lib/public/Log/BeforeMessageLoggedEvent.php @@ -28,7 +28,7 @@ /** * Even for when a log item is being logged * - * @since 26.0.7 + * @since 26.0.8 */ class BeforeMessageLoggedEvent extends Event { private int $level; @@ -36,7 +36,7 @@ class BeforeMessageLoggedEvent extends Event { private $message; /** - * @since 28.0.0 + * @since 26.0.8 */ public function __construct(string $app, int $level, $message) { $this->level = $level; @@ -48,7 +48,7 @@ public function __construct(string $app, int $level, $message) { * Get the level of the log item * * @return int - * @since 28.0.0 + * @since 26.0.8 */ public function getLevel(): int { return $this->level; @@ -59,7 +59,7 @@ public function getLevel(): int { * Get the app context of the log item * * @return string - * @since 28.0.0 + * @since 26.0.8 */ public function getApp(): string { return $this->app; @@ -70,7 +70,7 @@ public function getApp(): string { * Get the message of the log item * * @return string - * @since 28.0.0 + * @since 26.0.8 */ public function getMessage(): string { return $this->message;