aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2023-05-26 18:57:42 +0200
committerRobin Appelman <robin@icewind.nl>2023-06-13 18:45:48 +0200
commitd9fa7b1e28fe7a12d9dd8a84a9ee546cc0f2cfb8 (patch)
tree1c0f3bdd580dc706a19f72b581f6ecf468af7572 /lib/private
parentaedb4bef1e6b012637d822ed25948d1a8b3ded4d (diff)
downloadnextcloud-server-d9fa7b1e28fe7a12d9dd8a84a9ee546cc0f2cfb8.tar.gz
nextcloud-server-d9fa7b1e28fe7a12d9dd8a84a9ee546cc0f2cfb8.zip
emit an event when a message is logged
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/EventDispatcher/EventDispatcher.php7
-rw-r--r--lib/private/Log.php23
-rw-r--r--lib/private/Log/PsrLoggerAdapter.php5
3 files changed, 34 insertions, 1 deletions
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 c27c42fe271..1f248be1410 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;
}