From 1195e11a8a088ff35f506983c0496cb8af595e54 Mon Sep 17 00:00:00 2001 From: Bryan Davis Date: Sun, 22 Mar 2015 18:53:24 -0600 Subject: [PATCH] Move MWLogger classes to MediaWiki\Logger namespace Move the MWLogger PSR-3 logging related classes into the MediaWiki\Logger namespace. Create shim classes to ease migration of existing MWLoggerFactory usage to the namespaced classes. Bug: T93406 Change-Id: I359cc81fbd2dcf8937742311dcc7d3dee08747b0 --- RELEASE-NOTES-1.25 | 3 +- api.php | 4 +- autoload.php | 32 ++-- docs/logger.txt | 71 ++++++++ docs/mwlogger.txt | 71 -------- includes/DefaultSettings.php | 12 +- includes/GlobalFunctions.php | 13 +- includes/MediaWiki.php | 6 +- .../{legacy/Logger.php => LegacyLogger.php} | 14 +- .../logger/{legacy/Spi.php => LegacySpi.php} | 13 +- includes/debug/logger/Logger.php | 72 -------- .../logger/{Factory.php => LoggerFactory.php} | 40 ++-- .../{monolog/Spi.php => MonologSpi.php} | 19 +- includes/debug/logger/NullSpi.php | 13 +- includes/debug/logger/Shims.php | 171 ++++++++++++++++++ includes/debug/logger/Spi.php | 8 +- .../debug/logger/monolog/LegacyFormatter.php | 13 +- .../{Handler.php => LegacyHandler.php} | 17 +- .../debug/logger/monolog/SyslogHandler.php | 4 +- .../{Processor.php => WikiProcessor.php} | 4 +- includes/exception/HttpError.php | 4 +- includes/jobqueue/JobRunner.php | 3 +- includes/objectcache/ObjectCache.php | 6 +- includes/specials/SpecialRunJobs.php | 4 +- maintenance/runJobs.php | 4 +- maintenance/storage/recompressTracked.php | 4 +- .../LoggerTest.php => LegacyLoggerTest.php} | 14 +- 27 files changed, 403 insertions(+), 236 deletions(-) create mode 100644 docs/logger.txt delete mode 100644 docs/mwlogger.txt rename includes/debug/logger/{legacy/Logger.php => LegacyLogger.php} (98%) rename includes/debug/logger/{legacy/Spi.php => LegacySpi.php} (82%) delete mode 100644 includes/debug/logger/Logger.php rename includes/debug/logger/{Factory.php => LoggerFactory.php} (70%) rename includes/debug/logger/{monolog/Spi.php => MonologSpi.php} (93%) create mode 100644 includes/debug/logger/Shims.php rename includes/debug/logger/monolog/{Handler.php => LegacyHandler.php} (94%) rename includes/debug/logger/monolog/{Processor.php => WikiProcessor.php} (95%) rename tests/phpunit/includes/debug/logging/{legacy/LoggerTest.php => LegacyLoggerTest.php} (88%) diff --git a/RELEASE-NOTES-1.25 b/RELEASE-NOTES-1.25 index 7b035acc89..1ec8b930b4 100644 --- a/RELEASE-NOTES-1.25 +++ b/RELEASE-NOTES-1.25 @@ -132,7 +132,8 @@ production. * The following libraries are now required: ** psr/log This library provides the interfaces set by the PSR-3 standard (http://www.php-fig.org/psr/psr-3/) - which are used by MediaWiki internally via the MWLoggerFactory class. + which are used by MediaWiki internally via the + MediaWiki\Logger\LoggerFactory class. See the structured logging RfC (https://www.mediawiki.org/wiki/Requests_for_comment/Structured_logging) for more background information. ** cssjanus/cssjanus diff --git a/api.php b/api.php index 7788a36c2f..92127474fc 100644 --- a/api.php +++ b/api.php @@ -30,6 +30,8 @@ * @file */ +use MediaWiki\Logger\LegacyLogger; + // So extensions (and other code) can check whether they're running in API mode define( 'MW_API', true ); @@ -124,7 +126,7 @@ if ( $wgAPIRequestLog ) { } else { $items[] = "failed in ApiBeforeMain"; } - MWLoggerLegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog ); + LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog ); wfDebug( "Logged API request to $wgAPIRequestLog\n" ); } diff --git a/autoload.php b/autoload.php index 33967381f3..d646a0e8bb 100644 --- a/autoload.php +++ b/autoload.php @@ -694,17 +694,17 @@ $wgAutoloadLocalClasses = array( 'MWFunction' => __DIR__ . '/includes/utils/MWFunction.php', 'MWHookException' => __DIR__ . '/includes/Hooks.php', 'MWHttpRequest' => __DIR__ . '/includes/HttpFunctions.php', - 'MWLogger' => __DIR__ . '/includes/debug/logger/Logger.php', - 'MWLoggerFactory' => __DIR__ . '/includes/debug/logger/Factory.php', - 'MWLoggerLegacyLogger' => __DIR__ . '/includes/debug/logger/legacy/Logger.php', - 'MWLoggerLegacySpi' => __DIR__ . '/includes/debug/logger/legacy/Spi.php', - 'MWLoggerMonologHandler' => __DIR__ . '/includes/debug/logger/monolog/Handler.php', - 'MWLoggerMonologLegacyFormatter' => __DIR__ . '/includes/debug/logger/monolog/LegacyFormatter.php', - 'MWLoggerMonologProcessor' => __DIR__ . '/includes/debug/logger/monolog/Processor.php', - 'MWLoggerMonologSpi' => __DIR__ . '/includes/debug/logger/monolog/Spi.php', - 'MWLoggerMonologSyslogHandler' => __DIR__ . '/includes/debug/logger/monolog/SyslogHandler.php', - 'MWLoggerNullSpi' => __DIR__ . '/includes/debug/logger/NullSpi.php', - 'MWLoggerSpi' => __DIR__ . '/includes/debug/logger/Spi.php', + 'MWLogger' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerFactory' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerLegacyLogger' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerLegacySpi' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerMonologHandler' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerMonologLegacyFormatter' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerMonologProcessor' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerMonologSpi' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerMonologSyslogHandler' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerNullSpi' => __DIR__ . '/includes/debug/logger/Shims.php', + 'MWLoggerSpi' => __DIR__ . '/includes/debug/logger/Shims.php', 'MWMemcached' => __DIR__ . '/includes/objectcache/MemcachedClient.php', 'MWMessagePack' => __DIR__ . '/includes/libs/MWMessagePack.php', 'MWNamespace' => __DIR__ . '/includes/MWNamespace.php', @@ -737,6 +737,16 @@ $wgAutoloadLocalClasses = array( 'MediaWikiSite' => __DIR__ . '/includes/site/MediaWikiSite.php', 'MediaWikiTitleCodec' => __DIR__ . '/includes/title/MediaWikiTitleCodec.php', 'MediaWikiVersionFetcher' => __DIR__ . '/includes/MediaWikiVersionFetcher.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', + 'MediaWiki\\Logger\\MonologSpi' => __DIR__ . '/includes/debug/logger/MonologSpi.php', + 'MediaWiki\\Logger\\Monolog\\LegacyFormatter' => __DIR__ . '/includes/debug/logger/monolog/LegacyFormatter.php', + 'MediaWiki\\Logger\\Monolog\\LegacyHandler' => __DIR__ . '/includes/debug/logger/monolog/LegacyHandler.php', + 'MediaWiki\\Logger\\Monolog\\SyslogHandler' => __DIR__ . '/includes/debug/logger/monolog/SyslogHandler.php', + 'MediaWiki\\Logger\\Monolog\\WikiProcessor' => __DIR__ . '/includes/debug/logger/monolog/WikiProcessor.php', + 'MediaWiki\\Logger\\NullSpi' => __DIR__ . '/includes/debug/logger/NullSpi.php', + 'MediaWiki\\Logger\\Spi' => __DIR__ . '/includes/debug/logger/Spi.php', 'MemCachedClientforWiki' => __DIR__ . '/includes/objectcache/MemcachedClient.php', 'MemcLockManager' => __DIR__ . '/includes/filebackend/lockmanager/MemcLockManager.php', 'MemcachedBagOStuff' => __DIR__ . '/includes/objectcache/MemcachedBagOStuff.php', diff --git a/docs/logger.txt b/docs/logger.txt new file mode 100644 index 0000000000..89e620aff0 --- /dev/null +++ b/docs/logger.txt @@ -0,0 +1,71 @@ +MediaWiki\Logger\LoggerFactory implements a PSR-3 [0] compatible message +logging system. + +Named Psr\Log\LoggerInterface instances can be obtained from the +MediaWiki\Logger\LoggerFactory::getInstance() static method. +MediaWiki\Logger\LoggerFactory expects a class implementing the +MediaWiki\Logger\Spi interface to act as a factory for new +Psr\Log\LoggerInterface instances. + +The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An +SPI is an API intended to be implemented or extended by a third party. This +software design pattern is intended to enable framework extension and +replaceable components. It is specifically used in the +MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging +implementations to be easily integrated with MediaWiki. + +The service provider interface allows the backend logging library to be +implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the +classname of the default MediaWiki\Logger\Spi implementation to be loaded at +runtime. This can either be the name of a class implementing the +MediaWiki\Logger\Spi with a zero argument constructor or a callable that will +return an MediaWiki\Logger\Spi instance. Alternately the +MediaWiki\Logger\LoggerFactory::registerProvider() static method can be called +to inject an MediaWiki\Logger\Spi instance into the LoggerFactory and bypass +the use of the default configuration variable. + +The MediaWiki\Logger\LegacySpi class implements a service provider to generate +MediaWiki\Logger\LegacyLogger instances. The MediaWiki\Logger\LegacyLogger +class implements the PSR-3 logger interface and provides output and +configuration equivalent to the historic logging output of wfDebug, +wfDebugLog, wfLogDBError and wfErrorLog. The MediaWiki\Logger\LegacySpi class +is the default service provider configured in DefaultSettings.php. It's usage +should be transparent for users who are not ready or do not wish to switch to +a alternate logging platform. + +The MediaWiki\Logger\MonologSpi class implements a service provider to +generate Psr\Log\LoggerInterface instances that use the Monolog [1] logging +library. See the PHP docs (or source) for MediaWiki\Logger\MonologSpi for +details on the configuration of this provider. The default configuration +installs a null handler that will silently discard all logging events. The +documentation provided by the class describes a more feature rich logging +configuration. + +== Classes == +; MediaWiki\Logger\LoggerFactory +: Factory for Psr\Log\LoggerInterface loggers +; MediaWiki\Logger\Spi +: Service provider interface for MediaWiki\Logger\LoggerFactory +; MediaWiki\Logger\NullSpi +: MediaWiki\Logger\Spi for creating instances that discard all log events +; MediaWiki\Logger\LegacySpi +: Service provider for creating MediaWiki\Logger\LegacyLogger instances +; MediaWiki\Logger\LegacyLogger +: PSR-3 logger that mimics the historical output and configuration of wfDebug, + wfErrorLog and other global logging functions. +; MediaWiki\Logger\MonologSpi +: MediaWiki\Logger\Spi for creating instances backed by the monolog logging library +; MediaWiki\Logger\Monolog\LegacyHandler +: Monolog handler that replicates the udp2log and file logging + functionality of wfErrorLog() +; MediaWiki\Logger\Monolog\WikiProcessor +: Monolog log processer that adds host: wfHostname() and wiki: wfWikiID() + to all records + +== Globals == +; $wgMWLoggerDefaultSpi +: Specification for creating the default service provider interface to use + with MediaWiki\Logger\LoggerFactory + +[0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md +[1]: https://github.com/Seldaek/monolog diff --git a/docs/mwlogger.txt b/docs/mwlogger.txt deleted file mode 100644 index ecc3626db0..0000000000 --- a/docs/mwlogger.txt +++ /dev/null @@ -1,71 +0,0 @@ -MWLoggerFactory implements a PSR-3 [0] compatible message logging system. - -Named Psr\Log\LoggerInterface instances can be obtained from the -MWLoggerFactory::getInstance() static method. MWLoggerFactory expects a class -implementing the MWLoggerSpi interface to act as a factory for new -Psr\Log\LoggerInterface instances. - -The "Spi" in MWLoggerSpi stands for "service provider interface". A SPI is -an API intended to be implemented or extended by a third party. This software -design pattern is intended to enable framework extension and replaceable -components. It is specifically used in the MWLoggerFactory service to allow -alternate PSR-3 logging implementations to be easily integrated with -MediaWiki. - -The MWLoggerFactory::getInstance() static method is the means by which most -code acquires a Psr\Log\LoggerInterface instance. This in turn delegates -creation of Psr\Log\LoggerInterface instances to a class implementing the -MWLoggerSpi service provider interface. - -The service provider interface allows the backend logging library to be -implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the -classname of the default MWLoggerSpi implementation to be loaded at runtime. -This can either be the name of a class implementing the MWLoggerSpi with -a zero argument constructor or a callable that will return an MWLoggerSpi -instance. Alternately the MWLoggerFactory::registerProvider method can be -called to inject an MWLoggerSpi instance into MWLoggerFactory and bypass the -use of this configuration variable. - -The MWLoggerLegacySpi class implements a service provider to generate -MWLoggerLegacyLogger instances. The MWLoggerLegacyLogger class implements the -PSR-3 logger interface and provides output and configuration equivalent to the -historic logging output of wfDebug, wfDebugLog, wfLogDBError and wfErrorLog. -The MWLoggerLegacySpi class is the default service provider configured in -DefaultSettings.php. It's usage should be transparent for users who are not -ready or do not wish to switch to a alternate logging platform. - -The MWLoggerMonologSpi class implements a service provider to generate -Psr\Log\LoggerInterface instances that use the Monolog [1] logging library. -See the PHP docs (or source) for MWLoggerMonologSpi for details on the -configuration of this provider. The default configuration installs a null -handler that will silently discard all logging events. The documentation -provided by the class describes a more feature rich logging configuration. - -== Classes == -; MWLoggerFactory -: Factory for Psr\Log\LoggerInterface loggers -; MWLoggerSpi -: Service provider interface for MWLoggerFactory -; MWLoggerNullSpi -: MWLoggerSpi for creating instances that discard all log events -; MWLoggerLegacySpi -: Service provider for creating MWLoggerLegacyLogger instances -; MWLoggerLegacyLogger -: PSR-3 logger that mimics the historical output and configuration of wfDebug, - wfErrorLog and other global logging functions. -; MWLoggerMonologSpi -: MWLoggerSpi for creating instances backed by the monolog logging library -; MwLoggerMonologHandler -: Monolog handler that replicates the udp2log and file logging - functionality of wfErrorLog() -; MwLoggerMonologProcessor -: Monolog log processer that adds host: wfHostname() and wiki: wfWikiID() - to all records - -== Globals == -; $wgMWLoggerDefaultSpi -: Specification for creating the default service provider interface to use - with MWLoggerFactory - -[0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md -[1]: https://github.com/Seldaek/monolog diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 845c7f51ce..a5d66059ec 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -5303,16 +5303,16 @@ $wgDebugLogGroups = array(); * * The value should be an array suitable for use with * ObjectFactory::getObjectFromSpec(). The created object is expected to - * implement the MWLoggerSpi interface. See ObjectFactory for additional + * implement the MediaWiki\Logger\Spi interface. See ObjectFactory for additional * details. * - * Alternately the MWLoggerFactory::registerProvider method can be called to - * inject an MWLoggerSpi instance into MWLoggerFactory and bypass the use of - * this configuration variable entirely. + * Alternately the MediaWiki\Logger\LoggerFactory::registerProvider method can + * be called to inject an MediaWiki\Logger\Spi instance into the LoggerFactory + * and bypass the use of this configuration variable entirely. * * @par To completely disable logging: * @code - * $wgMWLoggerDefaultSpi = array( 'class' => 'MWLoggerNullSpi' ); + * $wgMWLoggerDefaultSpi = array( 'class' => '\\MediaWiki\\Logger\\NullSpi' ); * @endcode * * @since 1.25 @@ -5320,7 +5320,7 @@ $wgDebugLogGroups = array(); * @see MwLogger */ $wgMWLoggerDefaultSpi = array( - 'class' => 'MWLoggerLegacySpi', + 'class' => '\\MediaWiki\\Logger\\LegacySpi', ); /** diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index b2a2e3db4f..7c4ebc27e2 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -26,6 +26,7 @@ if ( !defined( 'MEDIAWIKI' ) ) { use Liuggio\StatsdClient\StatsdClient; use Liuggio\StatsdClient\Sender\SocketSender; +use MediaWiki\Logger\LoggerFactory; // Hide compatibility functions from Doxygen /// @cond @@ -1051,7 +1052,7 @@ function wfDebug( $text, $dest = 'all', array $context = array() ) { $context['prefix'] = $wgDebugLogPrefix; } - $logger = MWLoggerFactory::getInstance( 'wfDebug' ); + $logger = LoggerFactory::getInstance( 'wfDebug' ); $logger->debug( $text, $context ); } @@ -1151,7 +1152,7 @@ function wfDebugLog( $text = trim( $text ); - $logger = MWLoggerFactory::getInstance( $logGroup ); + $logger = LoggerFactory::getInstance( $logGroup ); $context['private'] = ( $dest === 'private' ); $logger->info( $text, $context ); } @@ -1165,7 +1166,7 @@ function wfDebugLog( * @param array $context Additional logging context data */ function wfLogDBError( $text, array $context = array() ) { - $logger = MWLoggerFactory::getInstance( 'wfLogDBError' ); + $logger = LoggerFactory::getInstance( 'wfLogDBError' ); $logger->error( trim( $text ), $context ); } @@ -1224,11 +1225,11 @@ function wfLogWarning( $msg, $callerOffset = 1, $level = E_USER_WARNING ) { * @param string $file Filename * @param array $context Additional logging context data * @throws MWException - * @deprecated since 1.25 Use MWLoggerLegacyLogger::emit or UDPTransport + * @deprecated since 1.25 Use MediaWiki\Logger\LegacyLogger::emit or UDPTransport */ function wfErrorLog( $text, $file, array $context = array() ) { wfDeprecated( __METHOD__, '1.25' ); - $logger = MWLoggerFactory::getInstance( 'wfErrorLog' ); + $logger = LoggerFactory::getInstance( 'wfErrorLog' ); $context['destination'] = $file; $logger->info( trim( $text ), $context ); } @@ -1303,7 +1304,7 @@ function wfLogProfilingData() { $ctx['output'] = $profiler->getOutput(); - $log = MWLoggerFactory::getInstance( 'profileoutput' ); + $log = LoggerFactory::getInstance( 'profileoutput' ); $log->info( "Elapsed: {elapsed}; URL: <{url}>\n{output}", $ctx ); } diff --git a/includes/MediaWiki.php b/includes/MediaWiki.php index 3f2cb6286c..68d03c8a56 100644 --- a/includes/MediaWiki.php +++ b/includes/MediaWiki.php @@ -20,6 +20,8 @@ * @file */ +use MediaWiki\Logger\LoggerFactory; + /** * The MediaWiki class is the helper class for the index.php entry point. */ @@ -478,7 +480,7 @@ class MediaWiki { $wgTitle = $title; $trxProfiler = Profiler::instance()->getTransactionProfiler(); - $trxProfiler->setLogger( MWLoggerFactory::getInstance( 'DBPerformance' ) ); + $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) ); // Aside from rollback, master queries should not happen on GET requests. // Periodic or "in passing" updates on GET should use the job queue. @@ -618,7 +620,7 @@ class MediaWiki { $n = intval( $jobRunRate ); } - $runJobsLogger = MWLoggerFactory::getInstance( 'runJobs' ); + $runJobsLogger = LoggerFactory::getInstance( 'runJobs' ); if ( !$this->config->get( 'RunJobsAsync' ) ) { // Fall back to running the job here while the user waits diff --git a/includes/debug/logger/legacy/Logger.php b/includes/debug/logger/LegacyLogger.php similarity index 98% rename from includes/debug/logger/legacy/Logger.php rename to includes/debug/logger/LegacyLogger.php index 610635db87..9cf104aa48 100644 --- a/includes/debug/logger/legacy/Logger.php +++ b/includes/debug/logger/LegacyLogger.php @@ -18,6 +18,13 @@ * @file */ +namespace MediaWiki\Logger; + +use MWDebug; +use Psr\Log\AbstractLogger; +use Psr\Log\LogLevel; +use UDPTransport; + /** * PSR-3 logger that mimics the historic implementation of MediaWiki's * wfErrorLog logging implementation. @@ -31,15 +38,12 @@ * See documentation in DefaultSettings.php for detailed explanations of each * variable. * - * @see MWLoggerFactory + * @see \MediaWiki\Logger\LoggerFactory * @since 1.25 * @author Bryan Davis * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ -use Psr\Log\AbstractLogger; -use Psr\Log\LogLevel; - -class MWLoggerLegacyLogger extends AbstractLogger { +class LegacyLogger extends AbstractLogger { /** * @var string $channel diff --git a/includes/debug/logger/legacy/Spi.php b/includes/debug/logger/LegacySpi.php similarity index 82% rename from includes/debug/logger/legacy/Spi.php rename to includes/debug/logger/LegacySpi.php index 79d4f24a73..1bf39e41b0 100644 --- a/includes/debug/logger/legacy/Spi.php +++ b/includes/debug/logger/LegacySpi.php @@ -18,23 +18,24 @@ * @file */ +namespace MediaWiki\Logger; + /** - * MWLoggerFactory service provider that creates MWLoggerLegacyLogger - * instances. + * LoggerFactory service provider that creates LegacyLogger instances. * * Usage: * @code * $wgMWLoggerDefaultSpi = array( - * 'class' => 'MWLoggerLegacySpi', + * 'class' => '\\MediaWiki\\Logger\\LegacySpi', * ); * @endcode * - * @see MWLoggerFactory + * @see \MediaWiki\Logger\LoggerFactory * @since 1.25 * @author Bryan Davis * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ -class MWLoggerLegacySpi implements MWLoggerSpi { +class LegacySpi implements Spi { /** * @var array $singletons @@ -50,7 +51,7 @@ class MWLoggerLegacySpi implements MWLoggerSpi { */ public function getLogger( $channel ) { if ( !isset( $this->singletons[$channel] ) ) { - $this->singletons[$channel] = new MWLoggerLegacyLogger( $channel ); + $this->singletons[$channel] = new LegacyLogger( $channel ); } return $this->singletons[$channel]; } diff --git a/includes/debug/logger/Logger.php b/includes/debug/logger/Logger.php deleted file mode 100644 index 27cf0cd073..0000000000 --- a/includes/debug/logger/Logger.php +++ /dev/null @@ -1,72 +0,0 @@ - * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ -class MWLoggerFactory { +class LoggerFactory { /** * Service provider. - * @var MWLoggerSpi $spi + * @var Spi $spi */ private static $spi; @@ -53,9 +56,9 @@ class MWLoggerFactory { * Register a service provider to create new \Psr\Log\LoggerInterface * instances. * - * @param MWLoggerSpi $provider Provider to register + * @param Spi $provider Provider to register */ - public static function registerProvider( MWLoggerSpi $provider ) { + public static function registerProvider( Spi $provider ) { self::$spi = $provider; } @@ -65,10 +68,10 @@ class MWLoggerFactory { * * If called before any service provider has been registered, it will * attempt to use the $wgMWLoggerDefaultSpi global to bootstrap - * MWLoggerSpi registration. $wgMWLoggerDefaultSpi is expected to be an + * Spi registration. $wgMWLoggerDefaultSpi is expected to be an * array usable by ObjectFactory::getObjectFromSpec() to create a class. * - * @return MWLoggerSpi + * @return Spi * @see registerProvider() * @see ObjectFactory::getObjectFromSpec() */ @@ -92,11 +95,14 @@ class MWLoggerFactory { */ public static function getInstance( $channel ) { if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) { - $message = <<PSR-3 logging library to be present. This library is not embedded directly in MediaWiki's git repository and must be installed separately by the end user. - -Please see mediawiki.org for help on installing the required components. -TXT; + $message = ( + 'MediaWiki requires the PSR-3 logging ' . + "library to be present. This library is not embedded directly in MediaWiki's " . + "git repository and must be installed separately by the end user.\n\n" . + 'Please see mediawiki.org for help on installing ' . + 'the required components.' + ); echo $message; trigger_error( $message, E_USER_ERROR ); die( 1 ); diff --git a/includes/debug/logger/monolog/Spi.php b/includes/debug/logger/MonologSpi.php similarity index 93% rename from includes/debug/logger/monolog/Spi.php rename to includes/debug/logger/MonologSpi.php index 68acf1df26..a07fdc4a8b 100644 --- a/includes/debug/logger/monolog/Spi.php +++ b/includes/debug/logger/MonologSpi.php @@ -18,8 +18,13 @@ * @file */ +namespace MediaWiki\Logger; + +use Monolog\Logger; +use ObjectFactory; + /** - * MWLoggerFactory service provider that creates loggers implemented by + * LoggerFactory service provider that creates loggers implemented by * Monolog. * * Configured using an array of configuration data with the keys 'loggers', @@ -30,11 +35,11 @@ * section. * * Configuration will most typically be provided in the $wgMWLoggerDefaultSpi - * global configuration variable used by MWLoggerFactory to construct its + * global configuration variable used by LoggerFactory to construct its * default SPI provider: * @code * $wgMWLoggerDefaultSpi = array( - * 'class' => 'MWLoggerMonologSpi', + * 'class' => '\\MediaWiki\\Logger\\MonologSpi', * 'args' => array( array( * 'loggers' => array( * '@default' => array( @@ -48,7 +53,7 @@ * ), * 'processors' => array( * 'wiki' => array( - * 'class' => 'MWLoggerMonologProcessor', + * 'class' => '\\MediaWiki\\Logger\\Monolog\\WikiProcessor', * ), * 'psr' => array( * 'class' => '\\Monolog\\Processor\\PsrLogMessageProcessor', @@ -81,7 +86,7 @@ * 'formatter' => 'logstash', * ), * 'udp2log' => array( - * 'class' => 'MWLoggerMonologHandler', + * 'class' => '\\MediaWiki\\Logger\\Monolog\\LegacyHandler', * 'args' => array( * 'udp://127.0.0.1:8420/mediawiki * ), @@ -106,7 +111,7 @@ * @author Bryan Davis * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ -class MWLoggerMonologSpi implements MWLoggerSpi { +class MonologSpi implements Spi { /** * @var array $singletons @@ -178,7 +183,7 @@ class MWLoggerMonologSpi implements MWLoggerSpi { * @return \Monolog\Logger */ protected function createLogger( $channel, $spec ) { - $obj = new \Monolog\Logger( $channel ); + $obj = new Logger( $channel ); if ( isset( $spec['processors'] ) ) { foreach ( $spec['processors'] as $processor ) { diff --git a/includes/debug/logger/NullSpi.php b/includes/debug/logger/NullSpi.php index 617842cc8f..a82d2c4c22 100644 --- a/includes/debug/logger/NullSpi.php +++ b/includes/debug/logger/NullSpi.php @@ -18,24 +18,27 @@ * @file */ +namespace MediaWiki\Logger; + +use Psr\Log\NullLogger; /** - * MWLoggerFactory service provider that creates \Psr\Log\NullLogger + * LoggerFactory service provider that creates \Psr\Log\NullLogger * instances. A NullLogger silently discards all log events sent to it. * * Usage: * @code * $wgMWLoggerDefaultSpi = array( - * 'class' => 'MWLoggerNullSpi', + * 'class' => '\\MediaWiki\\Logger\\NullSpi', * ); * @endcode * - * @see MWLoggerFactory + * @see \MediaWiki\Logger\LoggerFactory * @since 1.25 * @author Bryan Davis * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ -class MWLoggerNullSpi implements MWLoggerSpi { +class NullSpi implements Spi { /** * @var \Psr\Log\NullLogger $singleton @@ -44,7 +47,7 @@ class MWLoggerNullSpi implements MWLoggerSpi { public function __construct() { - $this->singleton = new \Psr\Log\NullLogger(); + $this->singleton = new NullLogger(); } diff --git a/includes/debug/logger/Shims.php b/includes/debug/logger/Shims.php new file mode 100644 index 0000000000..c78b0dce4d --- /dev/null +++ b/includes/debug/logger/Shims.php @@ -0,0 +1,171 @@ + * @copyright © 2014 Bryan Davis and Wikimedia Foundation. */ -interface MWLoggerSpi { +interface Spi { /** * Get a logger instance. diff --git a/includes/debug/logger/monolog/LegacyFormatter.php b/includes/debug/logger/monolog/LegacyFormatter.php index 67acf57d4a..9ec15cb85b 100644 --- a/includes/debug/logger/monolog/LegacyFormatter.php +++ b/includes/debug/logger/monolog/LegacyFormatter.php @@ -18,17 +18,22 @@ * @file */ +namespace MediaWiki\Logger\Monolog; + +use MediaWiki\Logger\LegacyLogger; +use Monolog\Formatter\NormalizerFormatter; + /** * Log message formatter that mimics the legacy log message formatting of * `wfDebug`, `wfDebugLog`, `wfLogDBError` and `wfErrorLog` global functions by - * delegating the formatting to MWLoggerLegacyLogger. + * delegating the formatting to \MediaWiki\Logger\LegacyLogger. * * @since 1.25 * @author Bryan Davis * @copyright © 2013 Bryan Davis and Wikimedia Foundation. - * @see MWLoggerLegacyLogger + * @see \MediaWiki\Logger\LegacyLogger */ -class MWLoggerMonologLegacyFormatter extends \Monolog\Formatter\NormalizerFormatter { +class LegacyFormatter extends NormalizerFormatter { public function __construct() { parent::__construct( 'c' ); @@ -36,7 +41,7 @@ class MWLoggerMonologLegacyFormatter extends \Monolog\Formatter\NormalizerFormat public function format( array $record ) { $normalized = parent::format( $record ); - return MWLoggerLegacyLogger::format( + return LegacyLogger::format( $normalized['channel'], $normalized['message'], $normalized ); } diff --git a/includes/debug/logger/monolog/Handler.php b/includes/debug/logger/monolog/LegacyHandler.php similarity index 94% rename from includes/debug/logger/monolog/Handler.php rename to includes/debug/logger/monolog/LegacyHandler.php index 9e7678d21c..8405819d86 100644 --- a/includes/debug/logger/monolog/Handler.php +++ b/includes/debug/logger/monolog/LegacyHandler.php @@ -18,6 +18,13 @@ * @file */ +namespace MediaWiki\Logger\Monolog; + +use LogicException; +use MediaWiki\Logger\LegacyLogger; +use Monolog\Handler\AbstractProcessingHandler; +use Monolog\Logger; +use UnexpectedValueException; /** * Log handler that replicates the behavior of MediaWiki's wfErrorLog() @@ -40,7 +47,7 @@ * @author Bryan Davis * @copyright © 2013 Bryan Davis and Wikimedia Foundation. */ -class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler { +class LegacyHandler extends AbstractProcessingHandler { /** * Log sink descriptor @@ -90,7 +97,7 @@ class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler public function __construct( $stream, $useLegacyFilter = false, - $level = \Monolog\Logger::DEBUG, + $level = Logger::DEBUG, $bubble = true ) { parent::__construct( $level, $bubble ); @@ -175,7 +182,7 @@ class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler protected function write( array $record ) { if ( $this->useLegacyFilter && - !MWLoggerLegacyLogger::shouldEmit( + !LegacyLogger::shouldEmit( $record['channel'], $record['message'], $record['level'], $record ) ) { @@ -213,7 +220,8 @@ class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler } socket_sendto( - $this->sink, $text, strlen( $text ), 0, $this->host, $this->port ); + $this->sink, $text, strlen( $text ), 0, $this->host, $this->port + ); } else { fwrite( $this->sink, $text ); @@ -232,5 +240,4 @@ class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler } $this->sink = null; } - } diff --git a/includes/debug/logger/monolog/SyslogHandler.php b/includes/debug/logger/monolog/SyslogHandler.php index 50c2fb5cd8..008efbc161 100644 --- a/includes/debug/logger/monolog/SyslogHandler.php +++ b/includes/debug/logger/monolog/SyslogHandler.php @@ -18,6 +18,8 @@ * @file */ +namespace MediaWiki\Logger\Monolog; + use Monolog\Handler\SyslogUdpHandler; use Monolog\Logger; @@ -44,7 +46,7 @@ use Monolog\Logger; * @author Bryan Davis * @copyright © 2015 Bryan Davis and Wikimedia Foundation. */ -class MWLoggerMonologSyslogHandler extends SyslogUdpHandler { +class SyslogHandler extends SyslogUdpHandler { /** * @var string $appname diff --git a/includes/debug/logger/monolog/Processor.php b/includes/debug/logger/monolog/WikiProcessor.php similarity index 95% rename from includes/debug/logger/monolog/Processor.php rename to includes/debug/logger/monolog/WikiProcessor.php index 4aa07f1b36..a52f63664b 100644 --- a/includes/debug/logger/monolog/Processor.php +++ b/includes/debug/logger/monolog/WikiProcessor.php @@ -18,6 +18,8 @@ * @file */ +namespace MediaWiki\Logger\Monolog; + /** * Injects `wfHostname()` and `wfWikiID()` in all records. * @@ -25,7 +27,7 @@ * @author Bryan Davis * @copyright © 2013 Bryan Davis and Wikimedia Foundation. */ -class MWLoggerMonologProcessor { +class WikiProcessor { /** * @param array $record diff --git a/includes/exception/HttpError.php b/includes/exception/HttpError.php index 051556623b..b81c57314c 100644 --- a/includes/exception/HttpError.php +++ b/includes/exception/HttpError.php @@ -18,6 +18,8 @@ * @file */ +use MediaWiki\Logger\LoggerFactory; + /** * Show an error that looks like an HTTP server error. * Replacement for wfHttpError(). @@ -81,7 +83,7 @@ class HttpError extends MWException { } private function doLog() { - $logger = MWLoggerFactory::getInstance( 'HttpError' ); + $logger = LoggerFactory::getInstance( 'HttpError' ); $content = $this->content; if ( $content instanceof Message ) { diff --git a/includes/jobqueue/JobRunner.php b/includes/jobqueue/JobRunner.php index edb491159d..b8c5d6cf78 100644 --- a/includes/jobqueue/JobRunner.php +++ b/includes/jobqueue/JobRunner.php @@ -21,6 +21,7 @@ * @ingroup JobQueue */ +use MediaWiki\Logger\LoggerFactory; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; @@ -58,7 +59,7 @@ class JobRunner implements LoggerAwareInterface { */ public function __construct( LoggerInterface $logger = null ) { if ( $logger === null ) { - $logger = MWLoggerFactory::getInstance( 'runJobs' ); + $logger = LoggerFactory::getInstance( 'runJobs' ); } $this->setLogger( $logger ); } diff --git a/includes/objectcache/ObjectCache.php b/includes/objectcache/ObjectCache.php index 1f2c9c046c..2e47e24ad9 100644 --- a/includes/objectcache/ObjectCache.php +++ b/includes/objectcache/ObjectCache.php @@ -21,6 +21,8 @@ * @ingroup Cache */ +use MediaWiki\Logger\LoggerFactory; + /** * Functions to get cache objects * @@ -82,11 +84,11 @@ class ObjectCache { */ static function newFromParams( $params ) { if ( isset( $params['loggroup'] ) ) { - $params['logger'] = MWLoggerFactory::getInstance( $params['loggroup'] ); + $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] ); } else { // For backwards-compatability with custom parameters, lets not // have all logging suddenly disappear - $params['logger'] = MWLoggerFactory::getInstance( 'objectcache' ); + $params['logger'] = LoggerFactory::getInstance( 'objectcache' ); } if ( isset( $params['factory'] ) ) { return call_user_func( $params['factory'], $params ); diff --git a/includes/specials/SpecialRunJobs.php b/includes/specials/SpecialRunJobs.php index 0d44d34358..8cf93670ba 100644 --- a/includes/specials/SpecialRunJobs.php +++ b/includes/specials/SpecialRunJobs.php @@ -22,6 +22,8 @@ * @author Aaron Schulz */ +use MediaWiki\Logger\LoggerFactory; + /** * Special page designed for running background tasks (internal use only) * @@ -89,7 +91,7 @@ class SpecialRunJobs extends UnlistedSpecialPage { // Do all of the specified tasks... if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) { - $runner = new JobRunner( MWLoggerFactory::getInstance( 'runJobs' ) ); + $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) ); $response = $runner->run( array( 'type' => $params['type'], 'maxJobs' => $params['maxjobs'] ? $params['maxjobs'] : 1, diff --git a/maintenance/runJobs.php b/maintenance/runJobs.php index 6abfb66bf3..3864e3c607 100644 --- a/maintenance/runJobs.php +++ b/maintenance/runJobs.php @@ -23,6 +23,8 @@ require_once __DIR__ . '/Maintenance.php'; +use MediaWiki\Logger\LoggerFactory; + /** * Maintenance script that runs pending jobs. * @@ -68,7 +70,7 @@ class RunJobs extends Maintenance { $json = ( $this->getOption( 'result' ) === 'json' ); - $runner = new JobRunner( MWLoggerFactory::getInstance( 'runJobs' ) ); + $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) ); if ( !$json ) { $runner->setDebugHandler( array( $this, 'debugInternal' ) ); } diff --git a/maintenance/storage/recompressTracked.php b/maintenance/storage/recompressTracked.php index eb7eca18a2..3562df6299 100644 --- a/maintenance/storage/recompressTracked.php +++ b/maintenance/storage/recompressTracked.php @@ -22,6 +22,8 @@ * @ingroup Maintenance ExternalStorage */ +use MediaWiki\Logger\LegacyLogger; + $optionsWithArgs = RecompressTracked::getOptionsWithArgs(); require __DIR__ . '/../commandLine.inc'; @@ -141,7 +143,7 @@ class RecompressTracked { $header .= "({$this->slaveId})"; } $header .= ' ' . wfWikiID(); - MWLoggerLegacyLogger::emit( sprintf( "%-50s %s\n", $header, $msg ), $file ); + LegacyLogger::emit( sprintf( "%-50s %s\n", $header, $msg ), $file ); } /** diff --git a/tests/phpunit/includes/debug/logging/legacy/LoggerTest.php b/tests/phpunit/includes/debug/logging/LegacyLoggerTest.php similarity index 88% rename from tests/phpunit/includes/debug/logging/legacy/LoggerTest.php rename to tests/phpunit/includes/debug/logging/LegacyLoggerTest.php index 66e9be4805..415fa0451b 100644 --- a/tests/phpunit/includes/debug/logging/legacy/LoggerTest.php +++ b/tests/phpunit/includes/debug/logging/LegacyLoggerTest.php @@ -17,17 +17,21 @@ * * @file */ + +namespace MediaWiki\Logger; + +use MediaWikiTestCase; use Psr\Log\LogLevel; -class MWLoggerLegacyLoggerTest extends MediaWikiTestCase { +class LegacyLoggerTest extends MediaWikiTestCase { /** - * @covers MWLoggerLegacyLogger::interpolate + * @covers LegacyLogger::interpolate * @dataProvider provideInterpolate */ public function testInterpolate( $message, $context, $expect ) { $this->assertEquals( - $expect, MWLoggerLegacyLogger::interpolate( $message, $context ) ); + $expect, LegacyLogger::interpolate( $message, $context ) ); } public function provideInterpolate() { @@ -68,14 +72,14 @@ class MWLoggerLegacyLoggerTest extends MediaWikiTestCase { } /** - * @covers MWLoggerLegacyLogger::shouldEmit + * @covers LegacyLogger::shouldEmit * @dataProvider provideShouldEmit */ public function testShouldEmit( $level, $config, $expected ) { $this->setMwGlobals( 'wgDebugLogGroups', array( 'fakechannel' => $config ) ); $this->assertEquals( $expected, - MWLoggerLegacyLogger::shouldEmit( 'fakechannel', 'some message', $level, array() ) + LegacyLogger::shouldEmit( 'fakechannel', 'some message', $level, array() ) ); } -- 2.20.1