Add ConsoleLogger, use it for eval.php -d
authorTim Starling <tstarling@wikimedia.org>
Thu, 27 Apr 2017 23:56:38 +0000 (09:56 +1000)
committerTim Starling <tstarling@wikimedia.org>
Fri, 28 Apr 2017 00:05:05 +0000 (10:05 +1000)
eval.php previously set $wgDebugLogFile to /dev/stdout. This had the
following problems:

* It doesn't work if the maintenance script is executed via sudo, since
  /dev/stdout is typically owned by the original user, so MW can't open
  it. Using php://stdout worked on HHVM but not PHP.
* Setting $wgDebugLogFile has no effect if the wiki uses MonologSpi.
* Setting $wgDebugLogFile has no effect on channels configured with
  $wgDebugLogGroups.
* stderr is a more appropriate place to send logging output.
* Writing to configuration variables is discouraged.

So, add ConsoleSpi, which is a very simple logging service provider
which sends all messages to stderr. This should be suitable for
debugging with eval.php or shell.php in WMF production or beta.

Change-Id: Ib0d6ce45e0cbecd58263fc4e360c63d4149acb3a

autoload.php
includes/debug/logger/ConsoleLogger.php [new file with mode: 0644]
includes/debug/logger/ConsoleSpi.php [new file with mode: 0644]
maintenance/eval.php
maintenance/shell.php

index f609ffc..1141c39 100644 (file)
@@ -876,6 +876,8 @@ $wgAutoloadLocalClasses = [
        'MediaWiki\\Linker\\LinkRenderer' => __DIR__ . '/includes/linker/LinkRenderer.php',
        'MediaWiki\\Linker\\LinkRendererFactory' => __DIR__ . '/includes/linker/LinkRendererFactory.php',
        'MediaWiki\\Linker\\LinkTarget' => __DIR__ . '/includes/linker/LinkTarget.php',
+       'MediaWiki\\Logger\\ConsoleLogger' => __DIR__ . '/includes/debug/logger/ConsoleLogger.php',
+       'MediaWiki\\Logger\\ConsoleSpi' => __DIR__ . '/includes/debug/logger/ConsoleSpi.php',
        'MediaWiki\\Logger\\LegacyLogger' => __DIR__ . '/includes/debug/logger/LegacyLogger.php',
        'MediaWiki\\Logger\\LegacySpi' => __DIR__ . '/includes/debug/logger/LegacySpi.php',
        'MediaWiki\\Logger\\LoggerFactory' => __DIR__ . '/includes/debug/logger/LoggerFactory.php',
diff --git a/includes/debug/logger/ConsoleLogger.php b/includes/debug/logger/ConsoleLogger.php
new file mode 100644 (file)
index 0000000..5a5e507
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+namespace MediaWiki\Logger;
+
+use Psr\Log\AbstractLogger;
+
+/**
+ * A logger which writes to the terminal. The output is supposed to be
+ * human-readable, and should be changed as necessary to better achieve that
+ * goal.
+ */
+class ConsoleLogger extends AbstractLogger {
+       public function __construct( $channel ) {
+               $this->channel = $channel;
+       }
+
+       public function log( $level, $message, array $context = [] ) {
+               fwrite( STDERR, "[$level] " .
+                       LegacyLogger::format( $this->channel, $message, $context ) );
+       }
+}
diff --git a/includes/debug/logger/ConsoleSpi.php b/includes/debug/logger/ConsoleSpi.php
new file mode 100644 (file)
index 0000000..e29b98d
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+namespace MediaWiki\Logger;
+
+class ConsoleSpi implements Spi {
+       public function __construct( $config = [] ) {
+       }
+
+       public function getLogger( $channel ) {
+               return new ConsoleLogger( $channel );
+       }
+}
index d98e5cd..ee8bdd6 100644 (file)
@@ -30,6 +30,9 @@
  * @ingroup Maintenance
  */
 
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\Logger\ConsoleSpi;
+
 $optionsWithArgs = [ 'd' ];
 
 require_once __DIR__ . "/commandLine.inc";
@@ -37,7 +40,7 @@ require_once __DIR__ . "/commandLine.inc";
 if ( isset( $options['d'] ) ) {
        $d = $options['d'];
        if ( $d > 0 ) {
-               $wgDebugLogFile = '/dev/stdout';
+               LoggerFactory::registerProvider( new ConsoleSpi );
        }
        if ( $d > 1 ) {
                $lb = wfGetLB();
index 47ef804..9f2306f 100644 (file)
@@ -34,6 +34,9 @@
  * @author GergÅ‘ Tisza <tgr.huwiki@gmail.com>
  */
 
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\Logger\ConsoleSpi;
+
 require_once __DIR__ . '/Maintenance.php';
 
 /**
@@ -46,7 +49,7 @@ class MediaWikiShell extends Maintenance {
                parent::__construct();
                $this->addOption( 'd',
                        'For back compatibility with eval.php. ' .
-                       '0 send debug to stdout. ' .
+                       '0 send debug to stderr. ' .
                        'With 1 additionally initialize database with debugging ',
                        false, true
                );
@@ -77,11 +80,9 @@ class MediaWikiShell extends Maintenance {
         * For back compatibility with eval.php
         */
        protected function setupLegacy() {
-               global $wgDebugLogFile;
-
                $d = intval( $this->getOption( 'd' ) );
                if ( $d > 0 ) {
-                       $wgDebugLogFile = 'php://stdout';
+                       LoggerFactory::registerProvider( new ConsoleSpi );
                }
                if ( $d > 1 ) {
                        # Set DBO_DEBUG (equivalent of $wgDebugDumpSql)