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