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.
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.
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
22 if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) {
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.
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.
29 trigger_error( $message, E_USER_ERROR
);
34 * PSR-3 logging service.
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.
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.
54 * @author Bryan Davis <bd808@wikimedia.org>
55 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
57 class MWLogger
implements \Psr\Log\LoggerInterface
{
61 * @var MWLoggerSpi $spi
63 protected static $spi;
67 * Wrapped PSR-3 logger instance.
69 * @var \Psr\Log\LoggerInterface $delegate
75 * @param \Psr\Log\LoggerInterface $logger
77 public function __construct( \Psr\Log\LoggerInterface
$logger ) {
78 $this->delegate
= $logger;
83 * Logs with an arbitrary level.
85 * @param string|int $level
86 * @param string $message
87 * @param array $context
89 public function log( $level, $message, array $context = array() ) {
90 $this->delegate
->log( $level, $message, $context );
97 * @param string $message
98 * @param array $context
100 public function emergency( $message, array $context = array() ) {
101 $this->log( \Psr\Log\LogLevel
::EMERGENCY
, $message, $context );
106 * Action must be taken immediately.
108 * Example: Entire website down, database unavailable, etc. This should
109 * trigger the SMS alerts and wake you up.
111 * @param string $message
112 * @param array $context
114 public function alert( $message, array $context = array() ) {
115 $this->log( \Psr\Log\LogLevel
::ALERT
, $message, $context );
120 * Critical conditions.
122 * Example: Application component unavailable, unexpected exception.
124 * @param string $message
125 * @param array $context
127 public function critical( $message, array $context = array( ) ) {
128 $this->log( \Psr\Log\LogLevel
::CRITICAL
, $message, $context );
133 * Runtime errors that do not require immediate action but should typically
134 * be logged and monitored.
136 * @param string $message
137 * @param array $context
139 public function error( $message, array $context = array( ) ) {
140 $this->log( \Psr\Log\LogLevel
::ERROR
, $message, $context );
145 * Exceptional occurrences that are not errors.
147 * Example: Use of deprecated APIs, poor use of an API, undesirable things
148 * that are not necessarily wrong.
150 * @param string $message
151 * @param array $context
153 public function warning( $message, array $context = array() ) {
154 $this->log( \Psr\Log\LogLevel
::WARNING
, $message, $context );
159 * Normal but significant events.
161 * @param string $message
162 * @param array $context
164 public function notice( $message, array $context = array() ) {
165 $this->log( \Psr\Log\LogLevel
::NOTICE
, $message, $context );
170 * Interesting events.
172 * Example: User logs in, SQL logs.
174 * @param string $message
175 * @param array $context
177 public function info( $message, array $context = array() ) {
178 $this->log( \Psr\Log\LogLevel
::INFO
, $message, $context );
183 * Detailed debug information.
185 * @param string $message
186 * @param array $context
188 public function debug( $message, array $context = array() ) {
189 $this->log( \Psr\Log\LogLevel
::DEBUG
, $message, $context );
194 * Register a service provider to create new MWLogger instances.
196 * @param MWLoggerSpi $provider Provider to register
198 public static function registerProvider( MWLoggerSpi
$provider ) {
199 self
::$spi = $provider;
204 * Get a named logger instance from the currently configured logger factory.
206 * @param string $channel Logger channel (name)
209 public static function getInstance( $channel ) {
210 if ( self
::$spi === null ) {
211 global $wgMWLoggerDefaultSpi;
212 $provider = ObjectFactory
::getObjectFromSpec(
213 $wgMWLoggerDefaultSpi
215 self
::registerProvider( $provider );
218 return self
::$spi->getLogger( $channel );