jenkins-bot has submitted this change and it was merged.

Change subject: Avoid MWDebug usage in DatabaseBase
......................................................................


Avoid MWDebug usage in DatabaseBase

This class is in /libs and cannot depend on all of MediaWiki.
Replace the call with a simple debug() call instead.

Also, make the legacy logger route/format errors from the two
new DB log types (DBConnection, DBQuery) to the old wfLogDBError
locations, including MWDebug::debugMsg().

Change-Id: I64895d3f5b9a000d8186ab6a6ffb4b76a7e9ff40
---
M includes/db/loadbalancer/LBFactoryMW.php
M includes/debug/logger/LegacyLogger.php
M includes/libs/rdbms/database/Database.php
M tests/phpunit/includes/db/DatabaseTestHelper.php
4 files changed, 37 insertions(+), 8 deletions(-)

Approvals:
  Aaron Schulz: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/db/loadbalancer/LBFactoryMW.php 
b/includes/db/loadbalancer/LBFactoryMW.php
index 69fd21d..f4d1777 100644
--- a/includes/db/loadbalancer/LBFactoryMW.php
+++ b/includes/db/loadbalancer/LBFactoryMW.php
@@ -52,8 +52,8 @@
                        'profiler' => Profiler::instance(),
                        'trxProfiler' => 
Profiler::instance()->getTransactionProfiler(),
                        'replLogger' => LoggerFactory::getInstance( 
'DBReplication' ),
-                       'queryLogger' => LoggerFactory::getInstance( 
'wfLogDBError' ),
-                       'connLogger' => LoggerFactory::getInstance( 
'wfLogDBError' ),
+                       'queryLogger' => LoggerFactory::getInstance( 'DBQuery' 
),
+                       'connLogger' => LoggerFactory::getInstance( 
'DBConnection' ),
                        'perfLogger' => LoggerFactory::getInstance( 
'DBPerformance' ),
                        'errorLogger' => [ MWExceptionHandler::class, 
'logException' ],
                        'cliMode' => $wgCommandLineMode,
diff --git a/includes/debug/logger/LegacyLogger.php 
b/includes/debug/logger/LegacyLogger.php
index 526b4ab..ef7a994 100644
--- a/includes/debug/logger/LegacyLogger.php
+++ b/includes/debug/logger/LegacyLogger.php
@@ -71,6 +71,14 @@
        ];
 
        /**
+        * @var array
+        */
+       protected static $dbChannels = [
+               'DBQuery' => true,
+               'DBConnection' => true
+       ];
+
+       /**
         * @param string $channel
         */
        public function __construct( $channel ) {
@@ -83,14 +91,29 @@
         * @param string|int $level
         * @param string $message
         * @param array $context
+        * @return null
         */
        public function log( $level, $message, array $context = [] ) {
-               if ( self::shouldEmit( $this->channel, $message, $level, 
$context ) ) {
-                       $text = self::format( $this->channel, $message, 
$context );
-                       $destination = self::destination( $this->channel, 
$message, $context );
+               if ( isset( self::$dbChannels[$this->channel] )
+                       && isset( self::$levelMapping[$level] )
+                       && self::$levelMapping[$level] >= LogLevel::ERROR
+               ) {
+                       // Format and write DB errors to the legacy locations
+                       $effectiveChannel = 'wfLogDBError';
+               } else {
+                       $effectiveChannel = $this->channel;
+               }
+
+               if ( self::shouldEmit( $effectiveChannel, $message, $level, 
$context ) ) {
+                       $text = self::format( $effectiveChannel, $message, 
$context );
+                       $destination = self::destination( $effectiveChannel, 
$message, $context );
                        self::emit( $text, $destination );
                }
-               if ( !isset( $context['private'] ) || !$context['private'] ) {
+               if ( $this->channel === 'DBQuery' && isset( $context['method'] )
+                       && isset( $context['master'] ) && isset( 
$context['runtime'] )
+               ) {
+                       MWDebug::query( $message, $context['method'], 
$context['master'], $context['runtime'] );
+               } elseif ( !isset( $context['private'] ) || 
!$context['private'] ) {
                        // Add to debug toolbar if not marked as "private"
                        MWDebug::debugMsg( $message, [ 'channel' => 
$this->channel ] + $context );
                }
@@ -298,6 +321,7 @@
         * @param string $channel
         * @param string $message
         * @param array $context
+        * @return null
         */
        protected static function formatAsWfDebugLog( $channel, $message, 
$context ) {
                $time = wfTimestamp( TS_DB );
@@ -432,7 +456,6 @@
        *
        * @param string $text
        * @param string $file Filename
-       * @throws MWException
        */
        public static function emit( $text, $file ) {
                if ( substr( $file, 0, 4 ) == 'udp:' ) {
diff --git a/includes/libs/rdbms/database/Database.php 
b/includes/libs/rdbms/database/Database.php
index de0de6e..1c2c0bd 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -903,7 +903,11 @@
                $this->trxProfiler->recordQueryCompletion(
                        $queryProf, $startTime, $isWrite, $this->affectedRows()
                );
-               MWDebug::query( $sql, $fname, $isMaster, $queryRuntime );
+               $this->queryLogger->debug( $sql, [
+                       'method' => $fname,
+                       'master' => $isMaster,
+                       'runtime' => $queryRuntime,
+               ] );
 
                return $ret;
        }
diff --git a/tests/phpunit/includes/db/DatabaseTestHelper.php 
b/tests/phpunit/includes/db/DatabaseTestHelper.php
index 63322cc..caa29bd 100644
--- a/tests/phpunit/includes/db/DatabaseTestHelper.php
+++ b/tests/phpunit/includes/db/DatabaseTestHelper.php
@@ -34,6 +34,8 @@
                $this->profiler = new ProfilerStub( [] );
                $this->trxProfiler = new TransactionProfiler();
                $this->cliMode = isset( $opts['cliMode'] ) ? $opts['cliMode'] : 
true;
+               $this->connLogger = new \Psr\Log\NullLogger();
+               $this->queryLogger = new \Psr\Log\NullLogger();
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/311185
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I64895d3f5b9a000d8186ab6a6ffb4b76a7e9ff40
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Parent5446 <tylerro...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to