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.
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.
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
21 if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) {
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.
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.
28 trigger_error( $message, E_USER_ERROR
);
33 * PSR-3 logging service.
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.
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.
52 * @author Bryan Davis <bd808@wikimedia.org>
53 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
55 class MWLogger
implements \Psr\Log\LoggerInterface
{
59 * @var MWLoggerSpi $spi
61 protected static $spi;
65 * Wrapped PSR-3 logger instance.
67 * @var \Psr\Log\LoggerInterface $delegate
73 * @param \Psr\Log\LoggerInterface $logger
75 public function __construct( \Psr\Log\LoggerInterface
$logger ) {
76 $this->delegate
= $logger;
81 * Logs with an arbitrary level.
83 * @param string|int $level
84 * @param string $message
85 * @param array $context
87 public function log( $level, $message, array $context = array() ) {
88 $this->delegate
->log( $level, $message, $context );
95 * @param string $message
96 * @param array $context
98 public function emergency( $message, array $context = array() ) {
99 $this->log( \Psr\Log\LogLevel
::EMERGENCY
, $message, $context );
104 * Action must be taken immediately.
106 * Example: Entire website down, database unavailable, etc. This should
107 * trigger the SMS alerts and wake you up.
109 * @param string $message
110 * @param array $context
112 public function alert( $message, array $context = array() ) {
113 $this->log( \Psr\Log\LogLevel
::ALERT
, $message, $context );
118 * Critical conditions.
120 * Example: Application component unavailable, unexpected exception.
122 * @param string $message
123 * @param array $context
125 public function critical( $message, array $context = array( ) ) {
126 $this->log( \Psr\Log\LogLevel
::CRITICAL
, $message, $context );
131 * Runtime errors that do not require immediate action but should typically
132 * be logged and monitored.
134 * @param string $message
135 * @param array $context
137 public function error( $message, array $context = array( ) ) {
138 $this->log( \Psr\Log\LogLevel
::ERROR
, $message, $context );
143 * Exceptional occurrences that are not errors.
145 * Example: Use of deprecated APIs, poor use of an API, undesirable things
146 * that are not necessarily wrong.
148 * @param string $message
149 * @param array $context
151 public function warning( $message, array $context = array() ) {
152 $this->log( \Psr\Log\LogLevel
::WARNING
, $message, $context );
157 * Normal but significant events.
159 * @param string $message
160 * @param array $context
162 public function notice( $message, array $context = array() ) {
163 $this->log( \Psr\Log\LogLevel
::NOTICE
, $message, $context );
168 * Interesting events.
170 * Example: User logs in, SQL logs.
172 * @param string $message
173 * @param array $context
175 public function info( $message, array $context = array() ) {
176 $this->log( \Psr\Log\LogLevel
::INFO
, $message, $context );
181 * Detailed debug information.
183 * @param string $message
184 * @param array $context
186 public function debug( $message, array $context = array() ) {
187 $this->log( \Psr\Log\LogLevel
::DEBUG
, $message, $context );
192 * Register a service provider to create new MWLogger instances.
194 * @param MWLoggerSpi $provider Provider to register
196 public static function registerProvider( MWLoggerSpi
$provider ) {
197 self
::$spi = $provider;
202 * Get the registered service provider.
204 * If called before any service provider has been registered, it will
205 * attempt to use the $wgMWLoggerDefaultSpi global to bootstrap
206 * MWLoggerSpi registration. $wgMWLoggerDefaultSpi is expected to be an
207 * array usable by ObjectFactory::getObjectFromSpec() to create a class.
209 * @return MWLoggerSpi
210 * @see registerProvider()
211 * @see ObjectFactory::getObjectFromSpec()
213 public static function getProvider() {
214 if ( self
::$spi === null ) {
215 global $wgMWLoggerDefaultSpi;
216 $provider = ObjectFactory
::getObjectFromSpec(
217 $wgMWLoggerDefaultSpi
219 self
::registerProvider( $provider );
225 * Get a named logger instance from the currently configured logger factory.
227 * @param string $channel Logger channel (name)
230 public static function getInstance( $channel ) {
231 return self
::getProvider()->getLogger( $channel );