Page MenuHomePhabricator

$wgMWLoggerDefaultSpi requires special usage, needs to be fixed or documented
Open, Needs TriagePublic

Description

I noticed that $wgMWLoggerDefaultSpi doesn't have any effect if I issue it at the end of my LocalSettings.php. My assumption is that the first time logging is called, global singleton stuff is set up which cannot be overwritten later. This seems fragile.


From description of T230270: $wgMWLoggerDefaultSpi is effectively ignored if log events occur before LocalSettings are loaded

It is possible for a LoggerFactory to be initialised before LocalSettings.php is loaded, leading to the $wgMWLoggerDefaultSpi value from DefaultSettings.php being “cached” permanently. It might be a good idea to either force reset of the factory's $spi property upon loading LocalSettings.php, or to check inside getInstance() whether $wgMWLoggerDefaultSpi was changed.

Steps to reproduce

  1. Define $wgMWLoggerDefaultSpi in LocalSettings.php.
  2. Have some code emit a log message before LocalSettings.php is loaded. In my case it was the WikiSEO extension installed using composer; the autoload script uses require_once instead of wfLoadExtension(), which makes the extension emit a warning.

Expected behaviour
Logging is performed using the specified logging subsystem.

Actual results
LegacySpi is used.

Event Timeline

awight raised the priority of this task from to Needs Triage.
awight updated the task description. (Show Details)
awight added a subscriber: awight.
Legoktm set Security to None.
Legoktm added a subscriber: bd808.

@awight's suspicion is correct. $wgMWLoggerDefaultSpi is read at the point that LoggerFactor::getInstance() is first called because MediaWiki wants to send a debug log message. At that point in time, the config is used to create the MediaWiki\Logger\Spi instance that will handle creating all subsequent \Psr\Log\LoggerInterface object instances. Changes to the bootstrapping config after this point, which is pretty early in the MediaWiki initialization lifecycle and almost certainly before reaching the end of LocalSettings.php processing, require forcing the logging service provider to be replaced inside MediaWiki\Logger\LoggerFactory.

MediaWiki-Vagrant does this with a hunk of code at the very end of it's configuration process:

// Ensure that full LoggerFactory configuration is applied
MediaWiki\Logger\LoggerFactory::registerProvider(
    ObjectFactory::getObjectFromSpec( $wgMWLoggerDefaultSpi )
);