Page MenuHomePhabricator

MWDebug toolbar log is empty when Monolog is enabled
Open, Needs TriagePublic

Description

If a wiki is configured to use Monolog (or any other PSR-3 logging system other than the default MediaWiki\Logger\LegacySpi) calls to wfDebug, wfDebugLog and direct use of named PSR-3 loggers will not be passed to MWDebug::debugMsg() for output as HTML, comments or Debug Toolbar data. Generically in the PSR-3 standard there is no way to handle MWDebug::debugMsg()'s expectation of receiving a copy of all log events.

MediaWiki\Logger\LegacyLogger (the current default PSR-3 implementation) forwards to MWDebug::debugMsg() for all calls LegacyLogger::log(), even if the log event will not otherwise be forwarded to an output channel. MediaWiki could provide a class implementing Monolog\Handler\HandlerInterface that could be bound to any/all logging channels and forward log events to MWDebug::debugMsg(). Instances of this special handler class could be automatically added to each logger acquired from MediaWiki\Logger\MonologSpi::getLogger() to replicate the legacy behavior.

Event Timeline

bd808 raised the priority of this task from to Low.
bd808 updated the task description. (Show Details)
bd808 added a project: MediaWiki-Debug-Logger.
bd808 added a subscriber: bd808.

Generically in the PSR-3 standard there is no way to handle MWDebug::debugMsg()'s expectation of receiving a copy of all log events.

There is (although I doubt it's worth the effort): make LoggerFactory return a proxy logger which calls debugMsg and then forwards to the real logger.

Krinkle renamed this task from Monolog logging has no way to interact with MWDebug::debugMsg() to MWDebug toolbar log is empty when Monolog is enabled.Jan 8 2021, 1:20 AM
Krinkle added a subscriber: Krinkle.

I've confirmed this is still an issue when configuring $wgMWLoggerDefaultSpi to use Monolog.

These are the settings I used, simplified based on current wmf-config:

mediawiki-docker-dev LocalSettings.mine.php
putenv( 'MW_LOG_DIR=/var/log/mediawiki' );
require_once "$IP/includes/DevelopmentSettings.php";

##
## Debug Logging
##

// ini_set( 'display_errors', 0 );
$wgDebugToolbar = true;
// $wgDevelopmentWarnings = true;

$wgMWLoggerDefaultSpi = [
	'class' => MediaWiki\Logger\MonologSpi::class,
	'args' => [ [
		'loggers' => [
			'@default' => [
				'processors' => [ 'wiki', 'psr', 'web' ],
				'handlers' => [ 'default' ],
			],
		],
		'processors' => [
			'wiki' => [
				'class' => \MediaWiki\Logger\Monolog\WikiProcessor::class,
			],
			'psr' => [
				'class' => \Monolog\Processor\PsrLogMessageProcessor::class,
			],
			'web' => [
				'class' => \Monolog\Processor\WebProcessor::class,
			],
		],
		'handlers' => [
			'default' => [
				'class' => \Monolog\Handler\StreamHandler::class,
				'args' => [ $wgDebugLogFile ?: '/tmp/mediawiki-debug.log' ],
				'formatter' => 'line',
			],
			'blackhole' => [
				'class' => \Monolog\Handler\NullHandler::class,
			],
		],
		'formatters' => [
			'line' => [
				'class' => \MediaWiki\Logger\Monolog\LineFormatter::class,
				'args' => [
					"%datetime% [%extra.reqId%] %extra.host% %extra.wiki% %extra.mwversion% %channel% %level_name%: %message% %context% %exception%\n",
					'Y-m-d H:i:s',
					true, // allowInlineLineBreaks
					true, // ignoreEmptyContextAndExtra
					true, // includeStacktraces
				],
			],
		],
	] ],
];

foreach ( $wgDebugLogGroups as $group => $target ) {
	if ( is_array( $target ) ) {
		// advanced settings like sample rate are ignored
		$target = $target['destination'];
	}
	if ( !$target ) {
		$wgMWLoggerDefaultSpi['args'][0]['loggers'][$group] = [ 'blackhole' ];
		continue;
	}
	$wgMWLoggerDefaultSpi['args'][0]['loggers'][$group] = [
		'processors' => [ 'wiki', 'psr', 'web' ],
		'handlers' => [ $group ],
	];
	$wgMWLoggerDefaultSpi['args'][0]['handlers'][$group] = [
		'class' => \Monolog\Handler\StreamHandler::class,
		'args' => [ $target ],
		'formatter' => 'line',
	];
}

Change 682756 had a related patch set uploaded (by Seb35; author: Seb35):

[mediawiki/core@master] Add LegacyLogger when $wgShowDebug is true.

https://gerrit.wikimedia.org/r/682756

The proposed patch adds a MediaWiki\Logger\LegacyLogger through a Monolog\Handler\PsrHandler when $wgShowDebug or $wgDebugToolbar is true, and the logs are now correctly displayed. I’m not sure it is the right way to do, feel free to modify the patch if you want.