diff --git a/config/config.sample.php b/config/config.sample.php index 0e6480cbf059..0f04b8dcdcb7 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -880,6 +880,11 @@ 'apps' => ['files_mediaviewer'], 'logfile' => '/tmp/mediaviewer.log' ], + [ + # special sql query logging + 'apps' => ['core/sql'], + 'logfile' => __DIR__ . '/../data/sql.jsonl' + ], ], /** diff --git a/lib/kernel.php b/lib/kernel.php index 05bd2b996aac..066bdb6dc746 100644 --- a/lib/kernel.php +++ b/lib/kernel.php @@ -64,6 +64,7 @@ * No, we can not put this class in its own file because it is used by * OC_autoload! */ +/** @codeCoverageIgnore */ class OC { /** * Associative array for autoloading. classname => filename @@ -551,12 +552,12 @@ public static function init() { @\ini_set('log_errors', 1); if (!\date_default_timezone_set('UTC')) { - \OC::$server->getLogger()->error('Could not set timezone to UTC'); - }; + self::$server->getLogger()->error('Could not set timezone to UTC'); + } - //try to configure php to enable big file uploads. - //this doesn´t work always depending on the webserver and php configuration. - //Let´s try to overwrite some defaults anyway + // try to configure php to enable big file uploads. + // this doesn't work always depending on the webserver and php configuration. + // Let´s try to overwrite some defaults anyway //try to set the maximum execution time to 60min @\set_time_limit(3600); @@ -685,6 +686,18 @@ public static function init() { $lockProvider = \OC::$server->getLockingProvider(); $lockProvider->releaseAll(); }); + $debug = \OC::$server->getConfig()->getSystemValue('debug', false); + if ($debug) { + \OC::$server->getShutdownHandler()->register(function () { + $queries = \OC::$server->getQueryLogger()->getQueries(); + \OC::$server->getLogger()->debug("SQL query log", [ + 'app' => 'core/sql', + 'extraFields' => [ + 'queries' => $queries + ] + ]); + }); + } // Check whether the sample configuration has been copied if ($systemConfig->getValue('copied_sample_config', false)) { diff --git a/lib/private/Diagnostics/Query.php b/lib/private/Diagnostics/Query.php index cc754d906b93..7c6d2ce6a1ed 100644 --- a/lib/private/Diagnostics/Query.php +++ b/lib/private/Diagnostics/Query.php @@ -24,21 +24,21 @@ use OCP\Diagnostics\IQuery; -class Query implements IQuery { - private $sql; +class Query implements IQuery, \JsonSerializable { + private string $sql; - private $params; + private array $params; - private $start; + private int $start; - private $end; + private int $end; /** * @param string $sql * @param array $params * @param int $start */ - public function __construct($sql, $params, $start) { + public function __construct(string $sql, $params, $start) { $this->sql = $sql; $this->params = $params; $this->start = $start; @@ -75,4 +75,14 @@ public function getStart() { public function getDuration() { return $this->end - $this->start; } + + public function jsonSerialize() { + return [ + 'query' => $this->sql, + 'parameters' => $this->params, + 'duration' => $this->getDuration(), + 'start' => $this->start, + 'end' => $this->end, + ]; + } } diff --git a/tests/lib/Diagnostics/QueryLoggerTest.php b/tests/lib/Diagnostics/QueryLoggerTest.php index 7fedf34abb7c..8f28400b1e87 100644 --- a/tests/lib/Diagnostics/QueryLoggerTest.php +++ b/tests/lib/Diagnostics/QueryLoggerTest.php @@ -31,7 +31,7 @@ class QueryLoggerTest extends TestCase { public function setUp(): void { parent::setUp(); - $this->logger = new QueryLogger(); + $this->logger = new QueryLogger(2444666668888888); } public function testQueryLogger(): void { @@ -48,5 +48,17 @@ public function testQueryLogger(): void { $queries = $this->logger->getQueries(); self::assertCount(1, $queries); + + # assert json serialize + self::assertEquals([ + 'query' => 'SELECT', + 'parameters' => [ + 'testuser', + 'count' + ], + 'duration' => 0, + 'start' => 2444666668888888, + 'end' => 2444666668888888 + ], $queries[0]->jsonSerialize()); } }