Skip to content

Commit

Permalink
feat: in debug mode write sql queries to the log file (#41093)
Browse files Browse the repository at this point in the history
* feat: in debug mode write sql queries to the log file

* chore: ignore code coverage on class OC

* test: Query::jsonSerialize()
  • Loading branch information
DeepDiver1975 authored Nov 15, 2023
1 parent 7a68659 commit f912fe4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
5 changes: 5 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,11 @@
'apps' => ['files_mediaviewer'],
'logfile' => '/tmp/mediaviewer.log'
],
[
# special sql query logging
'apps' => ['core/sql'],
'logfile' => __DIR__ . '/../data/sql.jsonl'
],
],

/**
Expand Down
23 changes: 18 additions & 5 deletions lib/kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
22 changes: 16 additions & 6 deletions lib/private/Diagnostics/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
];
}
}
14 changes: 13 additions & 1 deletion tests/lib/Diagnostics/QueryLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}

0 comments on commit f912fe4

Please sign in to comment.