Skip to content

Commit 9a299ed

Browse files
committed
exit log() early if no crashreporter registered
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 3fb1674 commit 9a299ed

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

lib/private/Log.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,34 @@
6262
* MonoLog is an example implementing this interface.
6363
*/
6464
class Log implements ILogger, IDataLogger {
65-
private IWriter $logger;
6665
private ?SystemConfig $config;
6766
private ?bool $logConditionSatisfied = null;
6867
private ?Normalizer $normalizer;
69-
private ?IRegistry $crashReporters;
7068
private ?IEventDispatcher $eventDispatcher;
7169

7270
/**
7371
* @param IWriter $logger The logger that should be used
74-
* @param SystemConfig $config the system config object
72+
* @param SystemConfig|null $config the system config object
7573
* @param Normalizer|null $normalizer
76-
* @param IRegistry|null $registry
74+
* @param IRegistry|null $crashReporters
7775
*/
7876
public function __construct(
79-
IWriter $logger,
77+
private IWriter $logger,
8078
SystemConfig $config = null,
8179
Normalizer $normalizer = null,
82-
IRegistry $registry = null
80+
private ?IRegistry $crashReporters = null
8381
) {
8482
// FIXME: Add this for backwards compatibility, should be fixed at some point probably
8583
if ($config === null) {
8684
$config = \OC::$server->getSystemConfig();
8785
}
8886

8987
$this->config = $config;
90-
$this->logger = $logger;
9188
if ($normalizer === null) {
9289
$this->normalizer = new Normalizer();
9390
} else {
9491
$this->normalizer = $normalizer;
9592
}
96-
$this->crashReporters = $registry;
9793
$this->eventDispatcher = null;
9894
}
9995

@@ -211,6 +207,9 @@ public function debug(string $message, array $context = []) {
211207
*/
212208
public function log(int $level, string $message, array $context = []) {
213209
$minLevel = $this->getLogLevel($context);
210+
if ($level < $minLevel && (($this->crashReporters?->hasReporters() ?? false) === false)) {
211+
return; // we already know that log will be fully ignored
212+
}
214213

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

@@ -241,9 +240,7 @@ public function log(int $level, string $message, array $context = []) {
241240
$this->crashReporters->delegateMessage($entry['message'], $messageContext);
242241
}
243242
} else {
244-
if ($this->crashReporters !== null) {
245-
$this->crashReporters->delegateBreadcrumb($entry['message'], 'log', $context);
246-
}
243+
$this->crashReporters?->delegateBreadcrumb($entry['message'], 'log', $context);
247244
}
248245
} catch (Throwable $e) {
249246
// make sure we dont hard crash if logging fails
@@ -329,8 +326,8 @@ public function logException(Throwable $exception, array $context = []) {
329326
$level = $context['level'] ?? ILogger::ERROR;
330327

331328
$minLevel = $this->getLogLevel($context);
332-
if ($level < $minLevel && ($this->crashReporters === null || !$this->crashReporters->hasReporters())) {
333-
return;
329+
if ($level < $minLevel && (($this->crashReporters?->hasReporters() ?? false) === false)) {
330+
return; // we already know that log will be fully ignored
334331
}
335332

336333
// if an error is raised before the autoloader is properly setup, we can't serialize exceptions

0 commit comments

Comments
 (0)