f5d2445387a14520489bf6cc338c55abde079f2e
[lhc/web/wiklou.git] / includes / debug / logger / Logger.php
1 <?php
2 /**
3 * @section LICENSE
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) {
23 $message = <<<TXT
24 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.
25
26 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.
27 TXT;
28 echo $message;
29 trigger_error( $message, E_USER_ERROR );
30 die( 1 );
31 }
32
33 /**
34 * PSR-3 logging service.
35 *
36 * This class provides a service interface for logging system events. The
37 * MWLogger class itself is intended to be a thin wrapper around another PSR-3
38 * compliant logging library. Creation of MWLogger instances is managed via
39 * the MWLogger::getInstance() static method which in turn delegates to the
40 * currently registered service provider.
41 *
42 * A service provider is any class implementing the MWLoggerSpi interface.
43 * There are two possible methods of registering a service provider. The
44 * MWLogger::registerProvider() static method can be called at any time to
45 * change the service provider. If MWLogger::getInstance() is called before
46 * any service provider has been registered, it will attempt to use the
47 * $wgMWLoggerDefaultSpi global to bootstrap MWLoggerSpi registration.
48 * $wgMWLoggerDefaultSpi can either be the name of a class implementing the
49 * MWLoggerSpi interface with a zero argument constructor or a callable that
50 * will return an MWLoggerSpi instance.
51 *
52 * @see MWLoggerSpi
53 * @since 1.25
54 * @author Bryan Davis <bd808@wikimedia.org>
55 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
56 */
57 class MWLogger implements \Psr\Log\LoggerInterface {
58
59 /**
60 * Service provider.
61 * @var MWLoggerSpi $spi
62 */
63 protected static $spi;
64
65
66 /**
67 * Wrapped PSR-3 logger instance.
68 *
69 * @var \Psr\Log\LoggerInterface $delegate
70 */
71 protected $delegate;
72
73
74 /**
75 * @param \Psr\Log\LoggerInterface $logger
76 */
77 public function __construct( \Psr\Log\LoggerInterface $logger ) {
78 $this->delegate = $logger;
79 }
80
81
82 /**
83 * Logs with an arbitrary level.
84 *
85 * @param string|int $level
86 * @param string $message
87 * @param array $context
88 */
89 public function log( $level, $message, array $context = array() ) {
90 $this->delegate->log( $level, $message, $context );
91 }
92
93
94 /**
95 * System is unusable.
96 *
97 * @param string $message
98 * @param array $context
99 */
100 public function emergency( $message, array $context = array() ) {
101 $this->log( \Psr\Log\LogLevel::EMERGENCY, $message, $context );
102 }
103
104
105 /**
106 * Action must be taken immediately.
107 *
108 * Example: Entire website down, database unavailable, etc. This should
109 * trigger the SMS alerts and wake you up.
110 *
111 * @param string $message
112 * @param array $context
113 */
114 public function alert( $message, array $context = array() ) {
115 $this->log( \Psr\Log\LogLevel::ALERT, $message, $context );
116 }
117
118
119 /**
120 * Critical conditions.
121 *
122 * Example: Application component unavailable, unexpected exception.
123 *
124 * @param string $message
125 * @param array $context
126 */
127 public function critical( $message, array $context = array( ) ) {
128 $this->log( \Psr\Log\LogLevel::CRITICAL, $message, $context );
129 }
130
131
132 /**
133 * Runtime errors that do not require immediate action but should typically
134 * be logged and monitored.
135 *
136 * @param string $message
137 * @param array $context
138 */
139 public function error( $message, array $context = array( ) ) {
140 $this->log( \Psr\Log\LogLevel::ERROR, $message, $context );
141 }
142
143
144 /**
145 * Exceptional occurrences that are not errors.
146 *
147 * Example: Use of deprecated APIs, poor use of an API, undesirable things
148 * that are not necessarily wrong.
149 *
150 * @param string $message
151 * @param array $context
152 */
153 public function warning( $message, array $context = array() ) {
154 $this->log( \Psr\Log\LogLevel::WARNING, $message, $context );
155 }
156
157
158 /**
159 * Normal but significant events.
160 *
161 * @param string $message
162 * @param array $context
163 */
164 public function notice( $message, array $context = array() ) {
165 $this->log( \Psr\Log\LogLevel::NOTICE, $message, $context );
166 }
167
168
169 /**
170 * Interesting events.
171 *
172 * Example: User logs in, SQL logs.
173 *
174 * @param string $message
175 * @param array $context
176 */
177 public function info( $message, array $context = array() ) {
178 $this->log( \Psr\Log\LogLevel::INFO, $message, $context );
179 }
180
181
182 /**
183 * Detailed debug information.
184 *
185 * @param string $message
186 * @param array $context
187 */
188 public function debug( $message, array $context = array() ) {
189 $this->log( \Psr\Log\LogLevel::DEBUG, $message, $context );
190 }
191
192
193 /**
194 * Register a service provider to create new MWLogger instances.
195 *
196 * @param MWLoggerSpi $provider Provider to register
197 */
198 public static function registerProvider( MWLoggerSpi $provider ) {
199 self::$spi = $provider;
200 }
201
202
203 /**
204 * Get a named logger instance from the currently configured logger factory.
205 *
206 * @param string $channel Logger channel (name)
207 * @return MWLogger
208 */
209 public static function getInstance( $channel ) {
210 if ( self::$spi === null ) {
211 global $wgMWLoggerDefaultSpi;
212 $provider = ObjectFactory::getObjectFromSpec(
213 $wgMWLoggerDefaultSpi
214 );
215 self::registerProvider( $provider );
216 }
217
218 return self::$spi->getLogger( $channel );
219 }
220
221 }