Browse Source

Merge pull request #43529 from nextcloud/enh/noid/leave-log-early-if-no-crash-reporter

tags/v29.0.0beta1
John Molakvoæ 3 months ago
parent
commit
5207274148
No account linked to committer's email address

+ 9
- 17
lib/private/EventDispatcher/EventDispatcher.php View File

@@ -33,29 +33,17 @@ use OCP\Broadcast\Events\IBroadcastEvent;
use OCP\EventDispatcher\ABroadcastedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IContainer;
use OCP\IServerContainer;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
use function get_class;

class EventDispatcher implements IEventDispatcher {
/** @var SymfonyDispatcher */
private $dispatcher;

/** @var IContainer */
private $container;

/** @var LoggerInterface */
private $logger;

public function __construct(SymfonyDispatcher $dispatcher,
IServerContainer $container,
LoggerInterface $logger) {
$this->dispatcher = $dispatcher;
$this->container = $container;
$this->logger = $logger;

public function __construct(
private SymfonyDispatcher $dispatcher,
private IServerContainer $container,
private LoggerInterface $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 || $this->logger instanceof Log\PsrLoggerAdapter) {
@@ -86,6 +74,10 @@ class EventDispatcher implements IEventDispatcher {
$this->addListener($eventName, $listener, $priority);
}

public function hasListeners(string $eventName): bool {
return $this->dispatcher->hasListeners($eventName);
}

/**
* @deprecated
*/

+ 16
- 20
lib/private/Log.php View File

@@ -62,24 +62,22 @@ use function strtr;
* MonoLog is an example implementing this interface.
*/
class Log implements ILogger, IDataLogger {
private IWriter $logger;
private ?SystemConfig $config;
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 SystemConfig|null $config the system config object
* @param Normalizer|null $normalizer
* @param IRegistry|null $registry
* @param IRegistry|null $crashReporters
*/
public function __construct(
IWriter $logger,
private IWriter $logger,
SystemConfig $config = null,
Normalizer $normalizer = null,
IRegistry $registry = null
private ?IRegistry $crashReporters = null
) {
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
if ($config === null) {
@@ -87,13 +85,11 @@ class Log implements ILogger, IDataLogger {
}

$this->config = $config;
$this->logger = $logger;
if ($normalizer === null) {
$this->normalizer = new Normalizer();
} else {
$this->normalizer = $normalizer;
}
$this->crashReporters = $registry;
$this->eventDispatcher = null;
}

@@ -211,15 +207,18 @@ class Log implements ILogger, IDataLogger {
*/
public function log(int $level, string $message, array $context = []) {
$minLevel = $this->getLogLevel($context);
if ($level < $minLevel
&& (($this->crashReporters?->hasReporters() ?? false) === false)
&& (($this->eventDispatcher?->hasListeners(BeforeMessageLoggedEvent::class) ?? false) === false)) {
return; // no crash reporter, no listeners, we can stop for lower log level
}

array_walk($context, [$this->normalizer, 'format']);

$app = $context['app'] ?? 'no app in context';
$entry = $this->interpolateMessage($context, $message);

if ($this->eventDispatcher) {
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry));
}
$this->eventDispatcher?->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry));

$hasBacktrace = isset($entry['exception']);
$logBacktrace = $this->config->getValue('log.backtrace', false);
@@ -241,9 +240,7 @@ class Log implements ILogger, IDataLogger {
$this->crashReporters->delegateMessage($entry['message'], $messageContext);
}
} else {
if ($this->crashReporters !== null) {
$this->crashReporters->delegateBreadcrumb($entry['message'], 'log', $context);
}
$this->crashReporters?->delegateBreadcrumb($entry['message'], 'log', $context);
}
} catch (Throwable $e) {
// make sure we dont hard crash if logging fails
@@ -329,8 +326,10 @@ class Log implements ILogger, IDataLogger {
$level = $context['level'] ?? ILogger::ERROR;

$minLevel = $this->getLogLevel($context);
if ($level < $minLevel && ($this->crashReporters === null || !$this->crashReporters->hasReporters())) {
return;
if ($level < $minLevel
&& (($this->crashReporters?->hasReporters() ?? false) === false)
&& (($this->eventDispatcher?->hasListeners(BeforeMessageLoggedEvent::class) ?? false) === false)) {
return; // no crash reporter, no listeners, we can stop for lower log level
}

// if an error is raised before the autoloader is properly setup, we can't serialize exceptions
@@ -346,12 +345,9 @@ class Log implements ILogger, IDataLogger {
$data = array_merge($serializer->serializeException($exception), $data);
$data = $this->interpolateMessage($data, isset($context['message']) && $context['message'] !== '' ? $context['message'] : ('Exception thrown: ' . get_class($exception)), 'CustomMessage');


array_walk($context, [$this->normalizer, 'format']);

if ($this->eventDispatcher) {
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $data));
}
$this->eventDispatcher?->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $data));

try {
if ($level >= $minLevel) {

+ 9
- 0
lib/public/EventDispatcher/IEventDispatcher.php View File

@@ -69,6 +69,15 @@ interface IEventDispatcher {
*/
public function addServiceListener(string $eventName, string $className, int $priority = 0): void;

/**
* @template T of \OCP\EventDispatcher\Event
* @param string $eventName preferably the fully-qualified class name of the Event sub class
*
* @return bool TRUE if event has registered listeners
* @since 29.0.0
*/
public function hasListeners(string $eventName): bool;

/**
* @template T of \OCP\EventDispatcher\Event
* @param string $eventName

Loading…
Cancel
Save