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
23 * PSR-3 logging service.
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.
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.
43 * @author Bryan Davis <bd808@wikimedia.org>
44 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
46 class MWLogger
implements \Psr\Log\LoggerInterface
{
50 * @var MWLoggerSpi $spi
52 protected static $spi;
56 * Wrapped PSR-3 logger instance.
58 * @var \Psr\Log\LoggerInterface $delegate
64 * @param \Psr\Log\LoggerInterface $logger
66 public function __construct( \Psr\Log\LoggerInterface
$logger ) {
67 $this->delegate
= $logger;
72 * Logs with an arbitrary level.
74 * @param string|int $level
75 * @param string $message
76 * @param array $context
78 public function log( $level, $message, array $context = array() ) {
79 $this->delegate
->log( $level, $message, $context );
86 * @param string $message
87 * @param array $context
89 public function emergency( $message, array $context = array() ) {
90 $this->log( \Psr\Log\LogLevel
::EMERGENCY
, $message, $context );
95 * Action must be taken immediately.
97 * Example: Entire website down, database unavailable, etc. This should
98 * trigger the SMS alerts and wake you up.
100 * @param string $message
101 * @param array $context
103 public function alert( $message, array $context = array() ) {
104 $this->log( \Psr\Log\LogLevel
::ALERT
, $message, $context );
109 * Critical conditions.
111 * Example: Application component unavailable, unexpected exception.
113 * @param string $message
114 * @param array $context
116 public function critical( $message, array $context = array( ) ) {
117 $this->log( \Psr\Log\LogLevel
::CRITICAL
, $message, $context );
122 * Runtime errors that do not require immediate action but should typically
123 * be logged and monitored.
125 * @param string $message
126 * @param array $context
128 public function error( $message, array $context = array( ) ) {
129 $this->log( \Psr\Log\LogLevel
::ERROR
, $message, $context );
134 * Exceptional occurrences that are not errors.
136 * Example: Use of deprecated APIs, poor use of an API, undesirable things
137 * that are not necessarily wrong.
139 * @param string $message
140 * @param array $context
142 public function warning( $message, array $context = array() ) {
143 $this->log( \Psr\Log\LogLevel
::WARNING
, $message, $context );
148 * Normal but significant events.
150 * @param string $message
151 * @param array $context
153 public function notice( $message, array $context = array() ) {
154 $this->log( \Psr\Log\LogLevel
::NOTICE
, $message, $context );
159 * Interesting events.
161 * Example: User logs in, SQL logs.
163 * @param string $message
164 * @param array $context
166 public function info( $message, array $context = array() ) {
167 $this->log( \Psr\Log\LogLevel
::INFO
, $message, $context );
172 * Detailed debug information.
174 * @param string $message
175 * @param array $context
177 public function debug( $message, array $context = array() ) {
178 $this->log( \Psr\Log\LogLevel
::DEBUG
, $message, $context );
183 * Register a service provider to create new MWLogger instances.
185 * @param MWLoggerSpi $provider Provider to register
187 public static function registerProvider( MWLoggerSpi
$provider ) {
188 self
::$spi = $provider;
193 * Get a named logger instance from the currently configured logger factory.
195 * @param string $channel Logger channel (name)
198 public static function getInstance( $channel ) {
199 if ( self
::$spi === null ) {
200 global $wgMWLoggerDefaultSpi;
201 if ( is_callable( $wgMWLoggerDefaultSpi ) ) {
202 $provider = $wgMWLoggerDefaultSpi();
204 $provider = new $wgMWLoggerDefaultSpi();
206 self
::registerProvider( $provider );
209 return self
::$spi->getLogger( $channel );