diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2020-12-29 10:50:53 +0100 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2020-12-29 10:46:56 +0000 |
commit | 6762e616f57dc434cc6298227706aca0a8decb2e (patch) | |
tree | d4130d9a8958c25953c0bd23425892ce1d00d905 /lib | |
parent | af77e89e67481c1e169b5952d1f633b555a80464 (diff) | |
download | nextcloud-server-6762e616f57dc434cc6298227706aca0a8decb2e.tar.gz nextcloud-server-6762e616f57dc434cc6298227706aca0a8decb2e.zip |
Avoid huge exception argument logging
In some cases it might happen that you have an argument that deep down
somewhere has an array with a lot of entries (think thousands). Now
before we would just happily print them all. Which would fill the log.
Now it will just print the first 5. And add a line that there are N
more.
If you are on debug level we will still print them all.
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Log.php | 2 | ||||
-rw-r--r-- | lib/private/Log/ExceptionSerializer.php | 24 |
2 files changed, 22 insertions, 4 deletions
diff --git a/lib/private/Log.php b/lib/private/Log.php index dffc3f5335f..5ca2135307e 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -315,7 +315,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'] ?? '--'; diff --git a/lib/private/Log/ExceptionSerializer.php b/lib/private/Log/ExceptionSerializer.php index 339a6604ed1..004016c5323 100644 --- a/lib/private/Log/ExceptionSerializer.php +++ b/lib/private/Log/ExceptionSerializer.php @@ -32,6 +32,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 = [ @@ -91,6 +92,13 @@ class ExceptionSerializer { 'imagecreatefromstring', ]; + /** @var SystemConfig */ + private $systemConfig; + + public function __construct(SystemConfig $systemConfig) { + $this->systemConfig = $systemConfig; + } + public const methodsWithSensitiveParametersByClass = [ SetupController::class => [ 'run', @@ -162,11 +170,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) { |