From c58afe45b06ce1e380f55e40c0becb3ef930f709 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 15 Sep 2018 00:02:12 +0100 Subject: [PATCH] debug: Allow the DBQuery channel to be used Follows-up 81e8d7af4129eb56 and e861191b2cdceeeecee. When using $wgShowDebug, $wgDebugComments, or $wgDebugLogFile locally, or when using the 'log' attribute with X-Wikimedia-Debug, all channels should be enabled and logged. But, the DBQuery logs are currently going nowhere. The code for MWDebug::query() is intercepting it, even if $wgDebugToolbar is not enabled. And after that, the code for wfLogDBError was intercepting it, again, even if $wgDBerrorLog is not enabled. Bug: T202764 Change-Id: I710c26a9e9c30fea20975d1bc24e1f0af077c2ad --- includes/debug/MWDebug.php | 7 +++---- includes/debug/logger/LegacyLogger.php | 28 ++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/includes/debug/MWDebug.php b/includes/debug/MWDebug.php index 2189498687..ae7f948f29 100644 --- a/includes/debug/MWDebug.php +++ b/includes/debug/MWDebug.php @@ -349,12 +349,11 @@ class MWDebug { * @param string $function * @param bool $isMaster * @param float $runTime Query run time - * @return int ID number of the query to pass to queryTime or -1 if the - * debugger is disabled + * @return bool True if debugger is enabled, false otherwise */ public static function query( $sql, $function, $isMaster, $runTime ) { if ( !self::$enabled ) { - return -1; + return false; } // Replace invalid UTF-8 chars with a square UTF-8 character @@ -389,7 +388,7 @@ class MWDebug { 'time' => $runTime, ]; - return count( self::$query ) - 1; + return true; } /** diff --git a/includes/debug/logger/LegacyLogger.php b/includes/debug/logger/LegacyLogger.php index d4a79d931f..d566a47a30 100644 --- a/includes/debug/logger/LegacyLogger.php +++ b/includes/debug/logger/LegacyLogger.php @@ -93,18 +93,38 @@ class LegacyLogger extends AbstractLogger { * @return null */ public function log( $level, $message, array $context = [] ) { + global $wgDBerrorLog; + if ( is_string( $level ) ) { $level = self::$levelMapping[$level]; } - if ( $this->channel === 'DBQuery' && isset( $context['method'] ) - && isset( $context['master'] ) && isset( $context['runtime'] ) + if ( $this->channel === 'DBQuery' + && isset( $context['method'] ) + && isset( $context['master'] ) + && isset( $context['runtime'] ) ) { - MWDebug::query( $message, $context['method'], $context['master'], $context['runtime'] ); - return; // only send profiling data to MWDebug profiling + // Also give the query information to the MWDebug tools + $enabled = MWDebug::query( + $message, + $context['method'], + $context['master'], + $context['runtime'] + ); + if ( $enabled ) { + // If we the toolbar was enabled, return early so that we don't + // also log the query to the main debug output. + return; + } } + // If this is a DB-related error, and the site has $wgDBerrorLog + // configured, rewrite the channel as wfLogDBError instead. + // Likewise, if the site does not use $wgDBerrorLog, it should + // configurable like any other channel via $wgDebugLogGroups + // or $wgMWLoggerDefaultSpi. if ( isset( self::$dbChannels[$this->channel] ) && $level >= self::$levelMapping[LogLevel::ERROR] + && $wgDBerrorLog ) { // Format and write DB errors to the legacy locations $effectiveChannel = 'wfLogDBError'; -- 2.20.1