Merge "Hide 'redirectedfrom' notice when printing articles"
[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 can either be the name of a class implementing the
48 * MWLoggerSpi interface with a zero argument constructor or a callable that
49 * will return an MWLoggerSpi instance.
50 *
51 * @see MWLoggerSpi
52 * @since 1.25
53 * @author Bryan Davis <bd808@wikimedia.org>
54 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
55 */
56 class MWLogger implements \Psr\Log\LoggerInterface {
57
58 /**
59 * Service provider.
60 * @var MWLoggerSpi $spi
61 */
62 protected static $spi;
63
64
65 /**
66 * Wrapped PSR-3 logger instance.
67 *
68 * @var \Psr\Log\LoggerInterface $delegate
69 */
70 protected $delegate;
71
72
73 /**
74 * @param \Psr\Log\LoggerInterface $logger
75 */
76 public function __construct( \Psr\Log\LoggerInterface $logger ) {
77 $this->delegate = $logger;
78 }
79
80
81 /**
82 * Logs with an arbitrary level.
83 *
84 * @param string|int $level
85 * @param string $message
86 * @param array $context
87 */
88 public function log( $level, $message, array $context = array() ) {
89 $this->delegate->log( $level, $message, $context );
90 }
91
92
93 /**
94 * System is unusable.
95 *
96 * @param string $message
97 * @param array $context
98 */
99 public function emergency( $message, array $context = array() ) {
100 $this->log( \Psr\Log\LogLevel::EMERGENCY, $message, $context );
101 }
102
103
104 /**
105 * Action must be taken immediately.
106 *
107 * Example: Entire website down, database unavailable, etc. This should
108 * trigger the SMS alerts and wake you up.
109 *
110 * @param string $message
111 * @param array $context
112 */
113 public function alert( $message, array $context = array() ) {
114 $this->log( \Psr\Log\LogLevel::ALERT, $message, $context );
115 }
116
117
118 /**
119 * Critical conditions.
120 *
121 * Example: Application component unavailable, unexpected exception.
122 *
123 * @param string $message
124 * @param array $context
125 */
126 public function critical( $message, array $context = array( ) ) {
127 $this->log( \Psr\Log\LogLevel::CRITICAL, $message, $context );
128 }
129
130
131 /**
132 * Runtime errors that do not require immediate action but should typically
133 * be logged and monitored.
134 *
135 * @param string $message
136 * @param array $context
137 */
138 public function error( $message, array $context = array( ) ) {
139 $this->log( \Psr\Log\LogLevel::ERROR, $message, $context );
140 }
141
142
143 /**
144 * Exceptional occurrences that are not errors.
145 *
146 * Example: Use of deprecated APIs, poor use of an API, undesirable things
147 * that are not necessarily wrong.
148 *
149 * @param string $message
150 * @param array $context
151 */
152 public function warning( $message, array $context = array() ) {
153 $this->log( \Psr\Log\LogLevel::WARNING, $message, $context );
154 }
155
156
157 /**
158 * Normal but significant events.
159 *
160 * @param string $message
161 * @param array $context
162 */
163 public function notice( $message, array $context = array() ) {
164 $this->log( \Psr\Log\LogLevel::NOTICE, $message, $context );
165 }
166
167
168 /**
169 * Interesting events.
170 *
171 * Example: User logs in, SQL logs.
172 *
173 * @param string $message
174 * @param array $context
175 */
176 public function info( $message, array $context = array() ) {
177 $this->log( \Psr\Log\LogLevel::INFO, $message, $context );
178 }
179
180
181 /**
182 * Detailed debug information.
183 *
184 * @param string $message
185 * @param array $context
186 */
187 public function debug( $message, array $context = array() ) {
188 $this->log( \Psr\Log\LogLevel::DEBUG, $message, $context );
189 }
190
191
192 /**
193 * Register a service provider to create new MWLogger instances.
194 *
195 * @param MWLoggerSpi $provider Provider to register
196 */
197 public static function registerProvider( MWLoggerSpi $provider ) {
198 self::$spi = $provider;
199 }
200
201
202 /**
203 * Get a named logger instance from the currently configured logger factory.
204 *
205 * @param string $channel Logger channel (name)
206 * @return MWLogger
207 */
208 public static function getInstance( $channel ) {
209 if ( self::$spi === null ) {
210 global $wgMWLoggerDefaultSpi;
211 $provider = ObjectFactory::getObjectFromSpec(
212 $wgMWLoggerDefaultSpi
213 );
214 self::registerProvider( $provider );
215 }
216
217 return self::$spi->getLogger( $channel );
218 }
219
220 }