From a59ed5f3de94a491b669c51d4f60529108b85b4b Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Fri, 28 Apr 2017 09:56:38 +1000 Subject: [PATCH] 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 --- autoload.php | 2 ++ includes/debug/logger/ConsoleLogger.php | 21 +++++++++++++++++++++ includes/debug/logger/ConsoleSpi.php | 11 +++++++++++ maintenance/eval.php | 5 ++++- maintenance/shell.php | 9 +++++---- 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 includes/debug/logger/ConsoleLogger.php create mode 100644 includes/debug/logger/ConsoleSpi.php diff --git a/autoload.php b/autoload.php index f609ffc7c1..1141c391fb 100644 --- a/autoload.php +++ b/autoload.php @@ -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 index 0000000000..5a5e507195 --- /dev/null +++ b/includes/debug/logger/ConsoleLogger.php @@ -0,0 +1,21 @@ +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 0000000000..e29b98d3b7 --- /dev/null +++ b/includes/debug/logger/ConsoleSpi.php @@ -0,0 +1,11 @@ + 0 ) { - $wgDebugLogFile = '/dev/stdout'; + LoggerFactory::registerProvider( new ConsoleSpi ); } if ( $d > 1 ) { $lb = wfGetLB(); diff --git a/maintenance/shell.php b/maintenance/shell.php index 47ef8045b1..9f2306fc3e 100644 --- a/maintenance/shell.php +++ b/maintenance/shell.php @@ -34,6 +34,9 @@ * @author Gergő Tisza */ +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) -- 2.20.1