Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception serializing #35970

Merged
merged 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,19 @@
* MonoLog is an example implementing this interface.
*/
class Log implements ILogger, IDataLogger {

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

/** @var SystemConfig */
private $config;

/** @var boolean|null cache the result of the log condition check for the request */
private $logConditionSatisfied = null;

/** @var Normalizer */
private $normalizer;

/** @var IRegistry */
private $crashReporters;
private IWriter $logger;
private ?SystemConfig $config;
private ?bool $logConditionSatisfied = null;
private ?Normalizer $normalizer;
private ?IRegistry $crashReporters;

/**
* @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 = 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 Down Expand Up @@ -312,6 +302,11 @@ public function logException(Throwable $exception, array $context = []) {
$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;
juliushaertl marked this conversation as resolved.
Show resolved Hide resolved
}

// if an error is raised before the autoloader is properly setup, we can't serialize exceptions
try {
$serializer = $this->getSerializer();
Expand All @@ -325,7 +320,6 @@ public function logException(Throwable $exception, array $context = []) {
$data = array_merge($serializer->serializeException($exception), $data);
$data = $this->interpolateMessage($data, $context['message'] ?? '--', 'CustomMessage');

$minLevel = $this->getLogLevel($context);

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

Expand Down
6 changes: 3 additions & 3 deletions lib/private/Log/ExceptionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ private function removeValuesFromArgs($args, $values) {
}

private function encodeTrace($trace) {
$filteredTrace = $this->filterTrace($trace);
return array_map(function (array $line) {
$trace = array_map(function (array $line) {
if (isset($line['args'])) {
$line['args'] = array_map([$this, 'encodeArg'], $line['args']);
}
return $line;
}, $filteredTrace);
}, $trace);
return $this->filterTrace($trace);
}

private function encodeArg($arg, $nestingLevel = 5) {
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Support/CrashReport/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ private function loadLazyProviders(): void {
}
}
}

public function hasReporters(): bool {
return !empty($this->lazyReporters) || !empty($this->reporters);
}
}
9 changes: 9 additions & 0 deletions lib/public/Support/CrashReport/IRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ public function delegateReport($exception, array $context = []);
* @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;
}