Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / debug / logger / LoggerFactory.php
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
21 namespace MediaWiki\Logger;
22
23 use 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 * @author Bryan Davis <bd808@wikimedia.org>
44 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
45 */
46 class LoggerFactory {
47
48 /**
49 * Service provider.
50 * @var \MediaWiki\Logger\Spi $spi
51 */
52 private static $spi;
53
54
55 /**
56 * Register a service provider to create new \\Psr\\Log\\LoggerInterface
57 * instances.
58 *
59 * @param \\MediaWiki\\Logger\\Spi $provider Provider to register
60 */
61 public static function registerProvider( Spi $provider ) {
62 self::$spi = $provider;
63 }
64
65
66 /**
67 * Get the registered service provider.
68 *
69 * If called before any service provider has been registered, it will
70 * attempt to use the $wgMWLoggerDefaultSpi global to bootstrap
71 * Spi registration. $wgMWLoggerDefaultSpi is expected to be an
72 * array usable by ObjectFactory::getObjectFromSpec() to create a class.
73 *
74 * @return \\MediaWiki\\Logger\\Spi
75 * @see registerProvider()
76 * @see ObjectFactory::getObjectFromSpec()
77 */
78 public static function getProvider() {
79 if ( self::$spi === null ) {
80 global $wgMWLoggerDefaultSpi;
81 $provider = ObjectFactory::getObjectFromSpec(
82 $wgMWLoggerDefaultSpi
83 );
84 self::registerProvider( $provider );
85 }
86 return self::$spi;
87 }
88
89
90 /**
91 * Get a named logger instance from the currently configured logger factory.
92 *
93 * @param string $channel Logger channel (name)
94 * @return \\Psr\\Log\\LoggerInterface
95 */
96 public static function getInstance( $channel ) {
97 if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) {
98 $message = (
99 'MediaWiki requires the <a href="https://github.com/php-fig/log">PSR-3 logging ' .
100 "library</a> to be present. This library is not embedded directly in MediaWiki's " .
101 "git repository and must be installed separately by the end user.\n\n" .
102 'Please see <a href="https://www.mediawiki.org/wiki/Download_from_Git' .
103 '#Fetch_external_libraries">mediawiki.org</a> for help on installing ' .
104 'the required components.'
105 );
106 echo $message;
107 trigger_error( $message, E_USER_ERROR );
108 die( 1 );
109 }
110
111 return self::getProvider()->getLogger( $channel );
112 }
113
114
115 /**
116 * Construction of utility class is not allowed.
117 */
118 private function __construct() {
119 // no-op
120 }
121 }