Merge pull request #52276 from nextcloud/feat/noid/log-query-parameters

pull/51309/head
Kate 2025-04-24 12:56:11 +07:00 committed by GitHub
commit 85b43f62a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 3 deletions

@ -2585,6 +2585,28 @@ $CONFIG = [
*/
'query_log_file' => '',
/**
* Prefix all queries with the requestid when set to `yes`
*
* Requires `query_log_file` to be set.
*/
'query_log_file_requestid' => '',
/**
* Add all query parameters to the query log entry when set to `yes`
*
* Requires `query_log_file` to be set.
* Warning: This will log sensitive data into a plain text file.
*/
'query_log_file_parameters' => '',
/**
* Add a backtrace to the query log entry when set to `yes`
*
* Requires `query_log_file` to be set.
*/
'query_log_file_backtrace' => '',
/**
* Log all redis requests into a file
*

@ -414,7 +414,7 @@ class Connection extends PrimaryReadReplicaConnection {
$sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
$this->logQueryToFile($sql, $params);
try {
return parent::executeQuery($sql, $params, $types, $qcp);
} catch (\Exception $e) {
@ -461,7 +461,7 @@ class Connection extends PrimaryReadReplicaConnection {
}
$sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
$this->logQueryToFile($sql, $params);
try {
return (int)parent::executeStatement($sql, $params, $types);
} catch (\Exception $e) {
@ -470,14 +470,19 @@ class Connection extends PrimaryReadReplicaConnection {
}
}
protected function logQueryToFile(string $sql): void {
protected function logQueryToFile(string $sql, array $params): void {
$logFile = $this->systemConfig->getValue('query_log_file');
if ($logFile !== '' && is_writable(dirname($logFile)) && (!file_exists($logFile) || is_writable($logFile))) {
$prefix = '';
if ($this->systemConfig->getValue('query_log_file_requestid') === 'yes') {
$prefix .= Server::get(IRequestId::class)->getId() . "\t";
}
$postfix = '';
if ($this->systemConfig->getValue('query_log_file_parameters') === 'yes') {
$postfix .= '; ' . json_encode($params);
}
if ($this->systemConfig->getValue('query_log_file_backtrace') === 'yes') {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_pop($trace);