summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2023-05-26 18:57:42 +0200
committerArthur Schiwon <blizzz@arthur-schiwon.de>2023-07-10 18:29:09 +0200
commit7d17d2517f616e78ebceba703c1dc3ca8d63e2ed (patch)
treeca93f8c03e83780a83bb59c35011474f7637ce4f
parent1b422df12ac8eb26514849fb117e0dcf58623b88 (diff)
downloadnextcloud-server-7d17d2517f616e78ebceba703c1dc3ca8d63e2ed.tar.gz
nextcloud-server-7d17d2517f616e78ebceba703c1dc3ca8d63e2ed.zip
emit an event when a message is logged
Signed-off-by: Robin Appelman <robin@icewind.nl>
m---------3rdparty0
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/EventDispatcher/EventDispatcher.php7
-rw-r--r--lib/private/Log.php23
-rw-r--r--lib/private/Log/PsrLoggerAdapter.php5
-rw-r--r--lib/public/Log/BeforeMessageLoggedEvent.php78
7 files changed, 114 insertions, 1 deletions
diff --git a/3rdparty b/3rdparty
-Subproject 6f18457f7dfcf23ce2877495a9323a144fd4a16
+Subproject 216b791c9081f06a4ed6581a436020647dd41bb
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index f35b9c94fbc..83fae3cfcc1 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -488,6 +488,7 @@ return array(
'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 8de7ef99d02..34cc99747ca 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -521,6 +521,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 2a306344923..d64ad88be7e 100644
--- a/lib/private/EventDispatcher/EventDispatcher.php
+++ b/lib/private/EventDispatcher/EventDispatcher.php
@@ -27,6 +27,7 @@ declare(strict_types=1);
*/
namespace OC\EventDispatcher;
+use OC\Log;
use Psr\Log\LoggerInterface;
use function get_class;
use OC\Broadcast\Events\BroadcastEvent;
@@ -54,6 +55,12 @@ class EventDispatcher implements IEventDispatcher {
$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 c139510a3b3..a83e652f8d3 100644
--- a/lib/private/Log.php
+++ b/lib/private/Log.php
@@ -39,6 +39,8 @@ namespace OC;
use Exception;
use Nextcloud\LogNormalizer\Normalizer;
use OC\AppFramework\Bootstrap\Coordinator;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Log\BeforeMessageLoggedEvent;
use OCP\Log\IDataLogger;
use Throwable;
use function array_merge;
@@ -64,6 +66,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
@@ -71,7 +74,12 @@ class Log implements ILogger, IDataLogger {
* @param Normalizer|null $normalizer
* @param IRegistry|null $registry
*/
- public function __construct(IWriter $logger, SystemConfig $config = null, Normalizer $normalizer = null, IRegistry $registry = null) {
+ public function __construct(
+ IWriter $logger,
+ SystemConfig $config = null,
+ Normalizer $normalizer = null,
+ IRegistry $registry = null
+ ) {
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
if ($config === null) {
$config = \OC::$server->getSystemConfig();
@@ -85,6 +93,11 @@ class Log implements ILogger, IDataLogger {
$this->normalizer = $normalizer;
}
$this->crashReporters = $registry;
+ $this->eventDispatcher = null;
+ }
+
+ public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
+ $this->eventDispatcher = $eventDispatcher;
}
/**
@@ -203,6 +216,10 @@ class Log implements ILogger, IDataLogger {
$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);
@@ -322,6 +339,10 @@ class Log implements ILogger, IDataLogger {
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 80c4c187b13..07a898e2528 100644
--- a/lib/private/Log/PsrLoggerAdapter.php
+++ b/lib/private/Log/PsrLoggerAdapter.php
@@ -26,6 +26,7 @@ declare(strict_types=1);
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 @@ final class PsrLoggerAdapter implements LoggerInterface, IDataLogger {
$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 00000000000..c185123d622
--- /dev/null
+++ b/lib/public/Log/BeforeMessageLoggedEvent.php
@@ -0,0 +1,78 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Log;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * Even for when a log item is being logged
+ *
+ * @since 27.0.1
+ */
+class BeforeMessageLoggedEvent extends Event {
+ private int $level;
+ private string $app;
+ private $message;
+
+ /**
+ * @since 27.0.1
+ */
+ 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 27.0.1
+ */
+ public function getLevel(): int {
+ return $this->level;
+ }
+
+
+ /**
+ * Get the app context of the log item
+ *
+ * @return string
+ * @since 27.0.1
+ */
+ public function getApp(): string {
+ return $this->app;
+ }
+
+
+ /**
+ * Get the message of the log item
+ *
+ * @return array
+ * @since 27.0.1
+ */
+ public function getMessage(): array {
+ return $this->message;
+ }
+}