perf(logging): Return early when log level does not match before serializing an exception

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2023-01-03 19:03:57 +01:00
parent 5e090d044d
commit 00b7575c89
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
3 changed files with 18 additions and 1 deletions

View File

@ -312,6 +312,11 @@ class Log implements ILogger, IDataLogger {
$app = $context['app'] ?? 'no app in context';
$level = $context['level'] ?? ILogger::ERROR;
$minLevel = $this->getLogLevel($context);
if ($level < $minLevel && ($this->crashReporters === null || !$this->crashReporters->hasReporters())) {
return;
}
// if an error is raised before the autoloader is properly setup, we can't serialize exceptions
try {
$serializer = $this->getSerializer();
@ -325,7 +330,6 @@ class Log implements ILogger, IDataLogger {
$data = array_merge($serializer->serializeException($exception), $data);
$data = $this->interpolateMessage($data, $context['message'] ?? '--', 'CustomMessage');
$minLevel = $this->getLogLevel($context);
array_walk($context, [$this->normalizer, 'format']);

View File

@ -147,4 +147,8 @@ class Registry implements IRegistry {
}
}
}
public function hasReporters(): bool {
return !empty($this->lazyReporters) || !empty($this->reporters);
}
}

View File

@ -81,4 +81,13 @@ interface IRegistry {
* @since 17.0.0
*/
public function delegateMessage(string $message, array $context = []): void;
/**
* Check if any reporter has been registered to delegate to
*
* @return bool
* @deprecated use internally only
* @since 26.0.0
*/
public function hasReporters(): bool;
}