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 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.
53 * @author Bryan Davis <bd808@wikimedia.org>
54 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
56 class MWLogger
implements \Psr\Log\LoggerInterface
{
60 * @var MWLoggerSpi $spi
62 protected static $spi;
66 * Wrapped PSR-3 logger instance.
68 * @var \Psr\Log\LoggerInterface $delegate
74 * @param \Psr\Log\LoggerInterface $logger
76 public function __construct( \Psr\Log\LoggerInterface
$logger ) {
77 $this->delegate
= $logger;
82 * Logs with an arbitrary level.
84 * @param string|int $level
85 * @param string $message
86 * @param array $context
88 public function log( $level, $message, array $context = array() ) {
89 $this->delegate
->log( $level, $message, $context );
96 * @param string $message
97 * @param array $context
99 public function emergency( $message, array $context = array() ) {
100 $this->log( \Psr\Log\LogLevel
::EMERGENCY
, $message, $context );
105 * Action must be taken immediately.
107 * Example: Entire website down, database unavailable, etc. This should
108 * trigger the SMS alerts and wake you up.
110 * @param string $message
111 * @param array $context
113 public function alert( $message, array $context = array() ) {
114 $this->log( \Psr\Log\LogLevel
::ALERT
, $message, $context );
119 * Critical conditions.
121 * Example: Application component unavailable, unexpected exception.
123 * @param string $message
124 * @param array $context
126 public function critical( $message, array $context = array( ) ) {
127 $this->log( \Psr\Log\LogLevel
::CRITICAL
, $message, $context );
132 * Runtime errors that do not require immediate action but should typically
133 * be logged and monitored.
135 * @param string $message
136 * @param array $context
138 public function error( $message, array $context = array( ) ) {
139 $this->log( \Psr\Log\LogLevel
::ERROR
, $message, $context );
144 * Exceptional occurrences that are not errors.
146 * Example: Use of deprecated APIs, poor use of an API, undesirable things
147 * that are not necessarily wrong.
149 * @param string $message
150 * @param array $context
152 public function warning( $message, array $context = array() ) {
153 $this->log( \Psr\Log\LogLevel
::WARNING
, $message, $context );
158 * Normal but significant events.
160 * @param string $message
161 * @param array $context
163 public function notice( $message, array $context = array() ) {
164 $this->log( \Psr\Log\LogLevel
::NOTICE
, $message, $context );
169 * Interesting events.
171 * Example: User logs in, SQL logs.
173 * @param string $message
174 * @param array $context
176 public function info( $message, array $context = array() ) {
177 $this->log( \Psr\Log\LogLevel
::INFO
, $message, $context );
182 * Detailed debug information.
184 * @param string $message
185 * @param array $context
187 public function debug( $message, array $context = array() ) {
188 $this->log( \Psr\Log\LogLevel
::DEBUG
, $message, $context );
193 * Register a service provider to create new MWLogger instances.
195 * @param MWLoggerSpi $provider Provider to register
197 public static function registerProvider( MWLoggerSpi
$provider ) {
198 self
::$spi = $provider;
203 * Get a named logger instance from the currently configured logger factory.
205 * @param string $channel Logger channel (name)
208 public static function getInstance( $channel ) {
209 if ( self
::$spi === null ) {
210 global $wgMWLoggerDefaultSpi;
211 $provider = ObjectFactory
::getObjectFromSpec(
212 $wgMWLoggerDefaultSpi
214 self
::registerProvider( $provider );
217 return self
::$spi->getLogger( $channel );