Merge "Use getHtmlCode() instead of getCode() to set the lang attribute"
[lhc/web/wiklou.git] / includes / debug / logger / Logger.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 if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) {
22 $message = <<<TXT
23 MediaWiki requires the <a href="https://github.com/php-fig/log">PSR-3 logging library</a> to be present. This library is not embedded directly in MediaWiki's git repository and must be installed separately by the end user.
24
25 Please see <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a> for help on installing the required components.
26 TXT;
27 echo $message;
28 trigger_error( $message, E_USER_ERROR );
29 die( 1 );
30 }
31
32 /**
33 * PSR-3 logging service.
34 *
35 * This class provides a service interface for logging system events. The
36 * MWLogger class itself is intended to be a thin wrapper around another PSR-3
37 * compliant logging library. Creation of MWLogger instances is managed via
38 * the MWLogger::getInstance() static method which in turn delegates to the
39 * currently registered service provider.
40 *
41 * A service provider is any class implementing the MWLoggerSpi interface.
42 * There are two possible methods of registering a service provider. The
43 * MWLogger::registerProvider() static method can be called at any time to
44 * change the service provider. If MWLogger::getInstance() is called before
45 * any service provider has been registered, it will attempt to use the
46 * $wgMWLoggerDefaultSpi global to bootstrap MWLoggerSpi registration.
47 * $wgMWLoggerDefaultSpi is expected to be an array usable by
48 * ObjectFactory::getObjectFromSpec() to create a class.
49 *
50 * @see MWLoggerSpi
51 * @since 1.25
52 * @author Bryan Davis <bd808@wikimedia.org>
53 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
54 */
55 class MWLogger implements \Psr\Log\LoggerInterface {
56
57 /**
58 * Service provider.
59 * @var MWLoggerSpi $spi
60 */
61 protected static $spi;
62
63
64 /**
65 * Wrapped PSR-3 logger instance.
66 *
67 * @var \Psr\Log\LoggerInterface $delegate
68 */
69 protected $delegate;
70
71
72 /**
73 * @param \Psr\Log\LoggerInterface $logger
74 */
75 public function __construct( \Psr\Log\LoggerInterface $logger ) {
76 $this->delegate = $logger;
77 }
78
79
80 /**
81 * Logs with an arbitrary level.
82 *
83 * @param string|int $level
84 * @param string $message
85 * @param array $context
86 */
87 public function log( $level, $message, array $context = array() ) {
88 $this->delegate->log( $level, $message, $context );
89 }
90
91
92 /**
93 * System is unusable.
94 *
95 * @param string $message
96 * @param array $context
97 */
98 public function emergency( $message, array $context = array() ) {
99 $this->log( \Psr\Log\LogLevel::EMERGENCY, $message, $context );
100 }
101
102
103 /**
104 * Action must be taken immediately.
105 *
106 * Example: Entire website down, database unavailable, etc. This should
107 * trigger the SMS alerts and wake you up.
108 *
109 * @param string $message
110 * @param array $context
111 */
112 public function alert( $message, array $context = array() ) {
113 $this->log( \Psr\Log\LogLevel::ALERT, $message, $context );
114 }
115
116
117 /**
118 * Critical conditions.
119 *
120 * Example: Application component unavailable, unexpected exception.
121 *
122 * @param string $message
123 * @param array $context
124 */
125 public function critical( $message, array $context = array( ) ) {
126 $this->log( \Psr\Log\LogLevel::CRITICAL, $message, $context );
127 }
128
129
130 /**
131 * Runtime errors that do not require immediate action but should typically
132 * be logged and monitored.
133 *
134 * @param string $message
135 * @param array $context
136 */
137 public function error( $message, array $context = array( ) ) {
138 $this->log( \Psr\Log\LogLevel::ERROR, $message, $context );
139 }
140
141
142 /**
143 * Exceptional occurrences that are not errors.
144 *
145 * Example: Use of deprecated APIs, poor use of an API, undesirable things
146 * that are not necessarily wrong.
147 *
148 * @param string $message
149 * @param array $context
150 */
151 public function warning( $message, array $context = array() ) {
152 $this->log( \Psr\Log\LogLevel::WARNING, $message, $context );
153 }
154
155
156 /**
157 * Normal but significant events.
158 *
159 * @param string $message
160 * @param array $context
161 */
162 public function notice( $message, array $context = array() ) {
163 $this->log( \Psr\Log\LogLevel::NOTICE, $message, $context );
164 }
165
166
167 /**
168 * Interesting events.
169 *
170 * Example: User logs in, SQL logs.
171 *
172 * @param string $message
173 * @param array $context
174 */
175 public function info( $message, array $context = array() ) {
176 $this->log( \Psr\Log\LogLevel::INFO, $message, $context );
177 }
178
179
180 /**
181 * Detailed debug information.
182 *
183 * @param string $message
184 * @param array $context
185 */
186 public function debug( $message, array $context = array() ) {
187 $this->log( \Psr\Log\LogLevel::DEBUG, $message, $context );
188 }
189
190
191 /**
192 * Register a service provider to create new MWLogger instances.
193 *
194 * @param MWLoggerSpi $provider Provider to register
195 */
196 public static function registerProvider( MWLoggerSpi $provider ) {
197 self::$spi = $provider;
198 }
199
200
201 /**
202 * Get a named logger instance from the currently configured logger factory.
203 *
204 * @param string $channel Logger channel (name)
205 * @return MWLogger
206 */
207 public static function getInstance( $channel ) {
208 if ( self::$spi === null ) {
209 global $wgMWLoggerDefaultSpi;
210 $provider = ObjectFactory::getObjectFromSpec(
211 $wgMWLoggerDefaultSpi
212 );
213 self::registerProvider( $provider );
214 }
215
216 return self::$spi->getLogger( $channel );
217 }
218
219 }