Tim Starling has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/350781 )

Change subject: Add ConsoleLogger, use it for eval.php -d
......................................................................

Add ConsoleLogger, use it for eval.php -d

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
---
M autoload.php
A includes/debug/logger/ConsoleLogger.php
A includes/debug/logger/ConsoleSpi.php
M maintenance/eval.php
M maintenance/shell.php
5 files changed, 43 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/81/350781/1

diff --git a/autoload.php b/autoload.php
index f609ffc..1141c39 100644
--- a/autoload.php
+++ b/autoload.php
@@ -876,6 +876,8 @@
        '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
index 0000000..5a5e507
--- /dev/null
+++ b/includes/debug/logger/ConsoleLogger.php
@@ -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
index 0000000..e29b98d
--- /dev/null
+++ b/includes/debug/logger/ConsoleSpi.php
@@ -0,0 +1,11 @@
+<?php
+namespace MediaWiki\Logger;
+
+class ConsoleSpi implements Spi {
+       public function __construct( $config = [] ) {
+       }
+
+       public function getLogger( $channel ) {
+               return new ConsoleLogger( $channel );
+       }
+}
diff --git a/maintenance/eval.php b/maintenance/eval.php
index d98e5cd..ee8bdd6 100644
--- a/maintenance/eval.php
+++ b/maintenance/eval.php
@@ -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 @@
 if ( isset( $options['d'] ) ) {
        $d = $options['d'];
        if ( $d > 0 ) {
-               $wgDebugLogFile = '/dev/stdout';
+               LoggerFactory::registerProvider( new ConsoleSpi );
        }
        if ( $d > 1 ) {
                $lb = wfGetLB();
diff --git a/maintenance/shell.php b/maintenance/shell.php
index 47ef804..9f2306f 100644
--- a/maintenance/shell.php
+++ b/maintenance/shell.php
@@ -34,6 +34,9 @@
  * @author Gergő Tisza <tgr.huw...@gmail.com>
  */
 
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\Logger\ConsoleSpi;
+
 require_once __DIR__ . '/Maintenance.php';
 
 /**
@@ -46,7 +49,7 @@
                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 @@
         * 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)

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib0d6ce45e0cbecd58263fc4e360c63d4149acb3a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Tim Starling <tstarl...@wikimedia.org>

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

Reply via email to