Browse Source

Merge pull request #24874 from nextcloud/enh/exception_logging

Avoid huge exception argument logging
tags/v21.0.0beta4
Roeland Jago Douma 3 years ago
parent
commit
c9fcf5f6b1
No account linked to committer's email address
2 changed files with 22 additions and 4 deletions
  1. 1
    1
      lib/private/Log.php
  2. 21
    3
      lib/private/Log/ExceptionSerializer.php

+ 1
- 1
lib/private/Log.php View File

@@ -314,7 +314,7 @@ class Log implements ILogger, IDataLogger {
$app = $context['app'] ?? 'no app in context';
$level = $context['level'] ?? ILogger::ERROR;

$serializer = new ExceptionSerializer();
$serializer = new ExceptionSerializer($this->config);
$data = $serializer->serializeException($exception);
$data['CustomMessage'] = $context['message'] ?? '--';


+ 21
- 3
lib/private/Log/ExceptionSerializer.php View File

@@ -33,6 +33,7 @@ use OC\Core\Controller\SetupController;
use OC\HintException;
use OC\Security\IdentityProof\Key;
use OC\Setup;
use OC\SystemConfig;

class ExceptionSerializer {
public const methodsWithSensitiveParameters = [
@@ -92,6 +93,13 @@ class ExceptionSerializer {
'imagecreatefromstring',
];

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

public function __construct(SystemConfig $systemConfig) {
$this->systemConfig = $systemConfig;
}

public const methodsWithSensitiveParametersByClass = [
SetupController::class => [
'run',
@@ -163,11 +171,21 @@ class ExceptionSerializer {
$data = get_object_vars($arg);
$data['__class__'] = get_class($arg);
return array_map([$this, 'encodeArg'], $data);
} elseif (is_array($arg)) {
}

if (is_array($arg)) {
// Only log the first 5 elements of an array unless we are on debug
if ((int)$this->systemConfig->getValue('loglevel', 2) !== 0) {
$elemCount = count($arg);
if ($elemCount > 5) {
$arg = array_slice($arg, 0, 5);
$arg[] = 'And ' . ($elemCount - 5) . ' more entries, set log level to debug to see all entries';
}
}
return array_map([$this, 'encodeArg'], $arg);
} else {
return $arg;
}

return $arg;
}

public function serializeException(\Throwable $exception) {

Loading…
Cancel
Save