rdbms: Restore debug toolbar "Queries" feature
[lhc/web/wiklou.git] / includes / debug / logger / LegacyLogger.php
index d566a47..974b044 100644 (file)
 namespace MediaWiki\Logger;
 
 use DateTimeZone;
+use Error;
 use Exception;
+use WikiMap;
 use MWDebug;
 use MWExceptionHandler;
 use Psr\Log\AbstractLogger;
 use Psr\Log\LogLevel;
+use Throwable;
 use UDPTransport;
 
 /**
- * PSR-3 logger that mimics the historic implementation of MediaWiki's
+ * PSR-3 logger that mimics the historic implementation of MediaWiki's former
  * wfErrorLog logging implementation.
  *
  * This logger is configured by the following global configuration variables:
@@ -99,16 +102,15 @@ class LegacyLogger extends AbstractLogger {
                        $level = self::$levelMapping[$level];
                }
                if ( $this->channel === 'DBQuery'
-                       && isset( $context['method'] )
-                       && isset( $context['master'] )
-                       && isset( $context['runtime'] )
+                       && $level === self::$levelMapping[LogLevel::DEBUG]
+                       && isset( $context['sql'] )
                ) {
                        // Also give the query information to the MWDebug tools
                        $enabled = MWDebug::query(
-                               $message,
+                               $context['sql'],
                                $context['method'],
-                               $context['master'],
-                               $context['runtime']
+                               $context['runtime'],
+                               $context['db_host']
                        );
                        if ( $enabled ) {
                                // If we the toolbar was enabled, return early so that we don't
@@ -165,10 +167,6 @@ class LegacyLogger extends AbstractLogger {
                        // specfied.
                        $shouldEmit = (bool)$wgDBerrorLog;
 
-               } elseif ( $channel === 'wfErrorLog' ) {
-                       // All messages on the wfErrorLog channel should be emitted.
-                       $shouldEmit = true;
-
                } elseif ( $channel === 'wfDebug' ) {
                        // wfDebug messages are emitted if a catch all logging file has
                        // been specified. Checked explicitly so that 'private' flagged
@@ -212,10 +210,9 @@ class LegacyLogger extends AbstractLogger {
        /**
         * Format a message.
         *
-        * Messages to the 'wfDebug', 'wfLogDBError' and 'wfErrorLog' channels
-        * receive special formatting to mimic the historic output of the functions
-        * of the same name. All other channel values are formatted based on the
-        * historic output of the `wfDebugLog()` global function.
+        * Messages to the 'wfDebug' and 'wfLogDBError' channels receive special formatting to mimic the
+        * historic output of the functions of the same name. All other channel values are formatted
+        * based on the historic output of the `wfDebugLog()` global function.
         *
         * @param string $channel
         * @param string $message
@@ -231,9 +228,6 @@ class LegacyLogger extends AbstractLogger {
                } elseif ( $channel === 'wfLogDBError' ) {
                        $text = self::formatAsWfLogDBError( $channel, $message, $context );
 
-               } elseif ( $channel === 'wfErrorLog' ) {
-                       $text = "{$message}\n";
-
                } elseif ( $channel === 'profileoutput' ) {
                        // Legacy wfLogProfilingData formatitng
                        $forward = '';
@@ -276,7 +270,7 @@ class LegacyLogger extends AbstractLogger {
                        $e = $context['exception'];
                        $backtrace = false;
 
-                       if ( $e instanceof Exception ) {
+                       if ( $e instanceof Throwable || $e instanceof Exception ) {
                                $backtrace = MWExceptionHandler::getRedactedTrace( $e );
 
                        } elseif ( is_array( $e ) && isset( $e['trace'] ) ) {
@@ -334,7 +328,7 @@ class LegacyLogger extends AbstractLogger {
                $date = $d->format( 'D M j G:i:s T Y' );
 
                $host = wfHostname();
-               $wiki = wfWikiID();
+               $wiki = WikiMap::getWikiIdFromDbDomain( WikiMap::getCurrentWikiDbDomain() );
 
                $text = "{$date}\t{$host}\t{$wiki}\t{$message}\n";
                return $text;
@@ -350,7 +344,7 @@ class LegacyLogger extends AbstractLogger {
         */
        protected static function formatAsWfDebugLog( $channel, $message, $context ) {
                $time = wfTimestamp( TS_DB );
-               $wiki = wfWikiID();
+               $wiki = WikiMap::getWikiIdFromDbDomain( WikiMap::getCurrentWikiDbDomain() );
                $host = wfHostname();
                $text = "{$time} {$host} {$wiki}: {$message}\n";
                return $text;
@@ -382,7 +376,7 @@ class LegacyLogger extends AbstractLogger {
         * @return string
         */
        protected static function flatten( $item ) {
-               if ( null === $item ) {
+               if ( $item === null ) {
                        return '[Null]';
                }
 
@@ -412,8 +406,9 @@ class LegacyLogger extends AbstractLogger {
                        return $item->format( 'c' );
                }
 
-               if ( $item instanceof Exception ) {
-                       return '[Exception ' . get_class( $item ) . '( ' .
+               if ( $item instanceof Throwable || $item instanceof Exception ) {
+                       $which = $item instanceof Error ? 'Error' : 'Exception';
+                       return '[' . $which . ' ' . get_class( $item ) . '( ' .
                                $item->getFile() . ':' . $item->getLine() . ') ' .
                                $item->getMessage() . ']';
                }