Skip to content

Commit

Permalink
Merge pull request #38490 from nextcloud/log-dispatch-event
Browse files Browse the repository at this point in the history
emit an event when a message is logged
  • Loading branch information
icewind1991 committed Jun 14, 2023
2 parents 7d851fb + d9fa7b1 commit e6959bd
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,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',
Expand Down
7 changes: 7 additions & 0 deletions lib/private/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
namespace OC\EventDispatcher;

use OC\Log;
use Psr\Log\LoggerInterface;
use function get_class;
use OC\Broadcast\Events\BroadcastEvent;
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 22 additions & 1 deletion lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
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;
Expand All @@ -64,14 +66,20 @@ 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
* @param SystemConfig $config the system config object
* @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();
Expand All @@ -85,6 +93,11 @@ public function __construct(IWriter $logger, SystemConfig $config = null, Normal
$this->normalizer = $normalizer;
}
$this->crashReporters = $registry;
$this->eventDispatcher = null;
}

public function setEventDispatcher(IEventDispatcher $eventDispatcher) {
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -203,6 +216,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);
Expand Down Expand Up @@ -322,6 +339,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) {
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Log/PsrLoggerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
78 changes: 78 additions & 0 deletions lib/public/Log/BeforeMessageLoggedEvent.php
Original file line number Diff line number Diff line change
@@ -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 28.0.0
*/
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;
}
}

0 comments on commit e6959bd

Please sign in to comment.