resourceloader: Make ResourceLoader logger aware
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 4 Jun 2015 03:52:45 +0000 (04:52 +0100)
committerBryanDavis <bdavis@wikimedia.org>
Thu, 4 Jun 2015 20:13:14 +0000 (20:13 +0000)
Change-Id: Ifa197cce1906bc0530dfa873dfde91abbe540637

includes/OutputPage.php
includes/resourceloader/ResourceLoader.php
includes/resourceloader/ResourceLoaderContext.php
load.php

index ba9fcba..2243996 100644 (file)
@@ -20,6 +20,8 @@
  * @file
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * This class should be covered by a general architecture document which does
  * not exist as of January 2011.  This is one of the Core classes and should
@@ -2737,7 +2739,10 @@ class OutputPage extends ContextSource {
         */
        public function getResourceLoader() {
                if ( is_null( $this->mResourceLoader ) ) {
-                       $this->mResourceLoader = new ResourceLoader( $this->getConfig() );
+                       $this->mResourceLoader = new ResourceLoader(
+                               $this->getConfig(),
+                               LoggerFactory::getInstance( 'resourceloader' )
+                       );
                }
                return $this->mResourceLoader;
        }
index 5bc9dc3..114a34f 100644 (file)
  * @author Trevor Parscal
  */
 
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
 /**
  * Dynamic JavaScript and CSS resource loading system.
  *
  * Most of the documentation is on the MediaWiki documentation wiki starting at:
  *    https://www.mediawiki.org/wiki/ResourceLoader
  */
-class ResourceLoader {
+class ResourceLoader implements LoggerAwareInterface {
        /** @var int */
        protected static $filterCacheVersion = 7;
 
@@ -77,6 +81,11 @@ class ResourceLoader {
         */
        protected $blobStore;
 
+       /**
+        * @var LoggerInterface
+        */
+       private $logger;
+
        /**
         * Load information stored in the database about modules.
         *
@@ -188,7 +197,7 @@ class ResourceLoader {
                }
 
                if ( !in_array( $filter, array( 'minify-js', 'minify-css' ) ) ) {
-                       wfDebugLog( 'resourceloader', __METHOD__ . ": Invalid filter: $filter" );
+                       $this->logger->info( __METHOD__ . ": Invalid filter: $filter" );
                        return $data;
                }
 
@@ -212,7 +221,7 @@ class ResourceLoader {
                                $cache->set( $key, $result );
                        } catch ( Exception $e ) {
                                MWExceptionHandler::logException( $e );
-                               wfDebugLog( 'resourceloader', __METHOD__ . ": minification failed: $e" );
+                               $this->logger->info( __METHOD__ . ": minification failed: $e" );
                                $this->errors[] = self::formatExceptionNoComment( $e );
                        }
                }
@@ -239,14 +248,18 @@ class ResourceLoader {
         * Register core modules and runs registration hooks.
         * @param Config|null $config
         */
-       public function __construct( Config $config = null ) {
+       public function __construct( Config $config = null, LoggerInterface $logger = null ) {
                global $IP;
 
-               if ( $config === null ) {
-                       wfDebug( __METHOD__ . ' was called without providing a Config instance' );
-                       $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+               if ( !$logger ) {
+                       $logger = new NullLogger();
                }
+               $this->setLogger( $logger );
 
+               if ( !$config ) {
+                       $this->logger->info( __METHOD__ . ' was called without providing a Config instance' );
+                       $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+               }
                $this->config = $config;
 
                // Add 'local' source first
@@ -276,6 +289,10 @@ class ResourceLoader {
                return $this->config;
        }
 
+       public function setLogger( LoggerInterface $logger ) {
+               $this->logger = $logger;
+       }
+
        /**
         * @param MessageBlobStore $blobStore
         * @since 1.25
@@ -633,7 +650,7 @@ class ResourceLoader {
                                // Do not allow private modules to be loaded from the web.
                                // This is a security issue, see bug 34907.
                                if ( $module->getGroup() === 'private' ) {
-                                       wfDebugLog( 'resourceloader', __METHOD__ . ": request for private module '$name' denied" );
+                                       $this->logger->info( __METHOD__ . ": request for private module '$name' denied" );
                                        $this->errors[] = "Cannot show private module \"$name\"";
                                        continue;
                                }
@@ -648,7 +665,7 @@ class ResourceLoader {
                        $this->preloadModuleInfo( array_keys( $modules ), $context );
                } catch ( Exception $e ) {
                        MWExceptionHandler::logException( $e );
-                       wfDebugLog( 'resourceloader', __METHOD__ . ": preloading module info failed: $e" );
+                       $this->logger->info( __METHOD__ . ": preloading module info failed: $e" );
                        $this->errors[] = self::formatExceptionNoComment( $e );
                }
 
@@ -658,7 +675,7 @@ class ResourceLoader {
                        $versionHash = $this->getCombinedVersion( $context, array_keys( $modules ) );
                } catch ( Exception $e ) {
                        MWExceptionHandler::logException( $e );
-                       wfDebugLog( 'resourceloader', __METHOD__ . ": calculating version hash failed: $e" );
+                       $this->logger->info( __METHOD__ . ": calculating version hash failed: $e" );
                        $this->errors[] = self::formatExceptionNoComment( $e );
                }
 
@@ -941,8 +958,7 @@ MESSAGE;
                                $blobs = $this->blobStore->get( $this, $modules, $context->getLanguage() );
                        } catch ( Exception $e ) {
                                MWExceptionHandler::logException( $e );
-                               wfDebugLog(
-                                       'resourceloader',
+                               $this->logger->info(
                                        __METHOD__ . ": pre-fetching blobs from MessageBlobStore failed: $e"
                                );
                                $this->errors[] = self::formatExceptionNoComment( $e );
@@ -1057,7 +1073,7 @@ MESSAGE;
                                }
                        } catch ( Exception $e ) {
                                MWExceptionHandler::logException( $e );
-                               wfDebugLog( 'resourceloader', __METHOD__ . ": generating module package failed: $e" );
+                               $this->logger->info( __METHOD__ . ": generating module package failed: $e" );
                                $this->errors[] = self::formatExceptionNoComment( $e );
 
                                // Respond to client with error-state instead of module implementation
index 66b4ee2..8eb5d8b 100644 (file)
@@ -22,6 +22,8 @@
  * @author Roan Kattouw
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * Object passed around to modules which contains information about the state
  * of a specific loader request
@@ -123,7 +125,8 @@ class ResourceLoaderContext {
         */
        public static function newDummyContext() {
                return new self( new ResourceLoader(
-                       ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
+                       ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
+                       LoggerFactory::getInstance( 'resourceloader' )
                ), new FauxRequest( array() ) );
        }
 
index e231807..22f62fe 100644 (file)
--- a/load.php
+++ b/load.php
@@ -22,6 +22,8 @@
  * @author Trevor Parscal
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 // Bail on old versions of PHP, or if composer has not been run yet to install
 // dependencies. Using dirname( __FILE__ ) here because __DIR__ is PHP5.3+.
 require_once dirname( __FILE__ ) . '/includes/PHPVersionCheck.php';
@@ -38,7 +40,10 @@ if ( !$wgRequest->checkUrlExtension() ) {
 // Respond to resource loading request.
 // foo()->bar() syntax is not supported in PHP4, and this file needs to *parse* in PHP4.
 $configFactory = ConfigFactory::getDefaultInstance();
-$resourceLoader = new ResourceLoader( $configFactory->makeConfig( 'main' ) );
+$resourceLoader = new ResourceLoader(
+       $configFactory->makeConfig( 'main' ),
+       LoggerFactory::getInstance( 'resourceloader' )
+);
 $resourceLoader->respond( new ResourceLoaderContext( $resourceLoader, $wgRequest ) );
 
 Profiler::instance()->setTemplated( true );