1<?php 2/** 3 * This program is free software; you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation; either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along 14 * with this program; if not, write to the Free Software Foundation, Inc., 15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 * http://www.gnu.org/copyleft/gpl.html 17 * 18 * @file 19 */ 20 21namespace MediaWiki\Logger; 22 23use Wikimedia\ObjectFactory; 24 25/** 26 * PSR-3 logger instance factory. 27 * 28 * Creation of \Psr\Log\LoggerInterface instances is managed via the 29 * LoggerFactory::getInstance() static method which in turn delegates to the 30 * currently registered service provider. 31 * 32 * A service provider is any class implementing the Spi interface. 33 * There are two possible methods of registering a service provider. The 34 * LoggerFactory::registerProvider() static method can be called at any time 35 * to change the service provider. If LoggerFactory::getInstance() is called 36 * before any service provider has been registered, it will attempt to use the 37 * $wgMWLoggerDefaultSpi global to bootstrap Spi registration. 38 * $wgMWLoggerDefaultSpi is expected to be an array usable by 39 * ObjectFactory::getObjectFromSpec() to create a class. 40 * 41 * @see \MediaWiki\Logger\Spi 42 * @since 1.25 43 * @copyright © 2014 Wikimedia Foundation and contributors 44 */ 45class LoggerFactory { 46 47 /** 48 * Service provider. 49 * @var \MediaWiki\Logger\Spi $spi 50 */ 51 private static $spi; 52 53 /** 54 * Register a service provider to create new \Psr\Log\LoggerInterface 55 * instances. 56 * 57 * @param \MediaWiki\Logger\Spi $provider Provider to register 58 */ 59 public static function registerProvider( Spi $provider ) { 60 self::$spi = $provider; 61 } 62 63 /** 64 * Get the registered service provider. 65 * 66 * If called before any service provider has been registered, it will 67 * attempt to use the $wgMWLoggerDefaultSpi global to bootstrap 68 * Spi registration. $wgMWLoggerDefaultSpi is expected to be an 69 * array usable by ObjectFactory::getObjectFromSpec() to create a class. 70 * 71 * @return \MediaWiki\Logger\Spi 72 * @see registerProvider() 73 * @see ObjectFactory::getObjectFromSpec() 74 */ 75 public static function getProvider() { 76 if ( self::$spi === null ) { 77 global $wgMWLoggerDefaultSpi; 78 $provider = ObjectFactory::getObjectFromSpec( 79 $wgMWLoggerDefaultSpi 80 ); 81 self::registerProvider( $provider ); 82 } 83 return self::$spi; 84 } 85 86 /** 87 * Get a named logger instance from the currently configured logger factory. 88 * 89 * @param string $channel Logger channel (name) 90 * @return \Psr\Log\LoggerInterface 91 */ 92 public static function getInstance( $channel ) { 93 return self::getProvider()->getLogger( $channel ); 94 } 95 96 /** 97 * Construction of utility class is not allowed. 98 */ 99 private function __construct() { 100 // no-op 101 } 102} 103