Merge "registration: Make it easier for other code to get extension metadata"
[lhc/web/wiklou.git] / includes / debug / logger / MonologSpi.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Logger;
22
23 use Monolog\Logger;
24 use ObjectFactory;
25
26 /**
27 * LoggerFactory service provider that creates loggers implemented by
28 * Monolog.
29 *
30 * Configured using an array of configuration data with the keys 'loggers',
31 * 'processors', 'handlers' and 'formatters'.
32 *
33 * The ['loggers']['@default'] configuration will be used to create loggers
34 * for any channel that isn't explicitly named in the 'loggers' configuration
35 * section.
36 *
37 * Configuration will most typically be provided in the $wgMWLoggerDefaultSpi
38 * global configuration variable used by LoggerFactory to construct its
39 * default SPI provider:
40 * @code
41 * $wgMWLoggerDefaultSpi = array(
42 * 'class' => '\\MediaWiki\\Logger\\MonologSpi',
43 * 'args' => array( array(
44 * 'loggers' => array(
45 * '@default' => array(
46 * 'processors' => array( 'wiki', 'psr', 'pid', 'uid', 'web' ),
47 * 'handlers' => array( 'stream' ),
48 * ),
49 * 'runJobs' => array(
50 * 'processors' => array( 'wiki', 'psr', 'pid' ),
51 * 'handlers' => array( 'stream' ),
52 * )
53 * ),
54 * 'processors' => array(
55 * 'wiki' => array(
56 * 'class' => '\\MediaWiki\\Logger\\Monolog\\WikiProcessor',
57 * ),
58 * 'psr' => array(
59 * 'class' => '\\Monolog\\Processor\\PsrLogMessageProcessor',
60 * ),
61 * 'pid' => array(
62 * 'class' => '\\Monolog\\Processor\\ProcessIdProcessor',
63 * ),
64 * 'uid' => array(
65 * 'class' => '\\Monolog\\Processor\\UidProcessor',
66 * ),
67 * 'web' => array(
68 * 'class' => '\\Monolog\\Processor\\WebProcessor',
69 * ),
70 * ),
71 * 'handlers' => array(
72 * 'stream' => array(
73 * 'class' => '\\Monolog\\Handler\\StreamHandler',
74 * 'args' => array( 'path/to/your.log' ),
75 * 'formatter' => 'line',
76 * ),
77 * 'redis' => array(
78 * 'class' => '\\Monolog\\Handler\\RedisHandler',
79 * 'args' => array( function() {
80 * $redis = new Redis();
81 * $redis->connect( '127.0.0.1', 6379 );
82 * return $redis;
83 * },
84 * 'logstash'
85 * ),
86 * 'formatter' => 'logstash',
87 * ),
88 * 'udp2log' => array(
89 * 'class' => '\\MediaWiki\\Logger\\Monolog\\LegacyHandler',
90 * 'args' => array(
91 * 'udp://127.0.0.1:8420/mediawiki
92 * ),
93 * 'formatter' => 'line',
94 * ),
95 * ),
96 * 'formatters' => array(
97 * 'line' => array(
98 * 'class' => '\\Monolog\\Formatter\\LineFormatter',
99 * ),
100 * 'logstash' => array(
101 * 'class' => '\\Monolog\\Formatter\\LogstashFormatter',
102 * 'args' => array( 'mediawiki', php_uname( 'n' ), null, '', 1 ),
103 * ),
104 * ),
105 * ) ),
106 * );
107 * @endcode
108 *
109 * @see https://github.com/Seldaek/monolog
110 * @since 1.25
111 * @author Bryan Davis <bd808@wikimedia.org>
112 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
113 */
114 class MonologSpi implements Spi {
115
116 /**
117 * @var array $singletons
118 */
119 protected $singletons;
120
121 /**
122 * Configuration for creating new loggers.
123 * @var array $config
124 */
125 protected $config;
126
127
128 /**
129 * @param array $config Configuration data.
130 */
131 public function __construct( array $config ) {
132 $this->config = $config;
133 $this->reset();
134 }
135
136
137 /**
138 * Reset internal caches.
139 *
140 * This is public for use in unit tests. Under normal operation there should
141 * be no need to flush the caches.
142 */
143 public function reset() {
144 $this->singletons = array(
145 'loggers' => array(),
146 'handlers' => array(),
147 'formatters' => array(),
148 'processors' => array(),
149 );
150 }
151
152
153 /**
154 * Get a logger instance.
155 *
156 * Creates and caches a logger instance based on configuration found in the
157 * $wgMWLoggerMonologSpiConfig global. Subsequent request for the same channel
158 * name will return the cached instance.
159 *
160 * @param string $channel Logging channel
161 * @return \Psr\Log\LoggerInterface Logger instance
162 */
163 public function getLogger( $channel ) {
164 if ( !isset( $this->singletons['loggers'][$channel] ) ) {
165 // Fallback to using the '@default' configuration if an explict
166 // configuration for the requested channel isn't found.
167 $spec = isset( $this->config['loggers'][$channel] ) ?
168 $this->config['loggers'][$channel] :
169 $this->config['loggers']['@default'];
170
171 $monolog = $this->createLogger( $channel, $spec );
172 $this->singletons['loggers'][$channel] = $monolog;
173 }
174
175 return $this->singletons['loggers'][$channel];
176 }
177
178
179 /**
180 * Create a logger.
181 * @param string $channel Logger channel
182 * @param array $spec Configuration
183 * @return \Monolog\Logger
184 */
185 protected function createLogger( $channel, $spec ) {
186 $obj = new Logger( $channel );
187
188 if ( isset( $spec['processors'] ) ) {
189 foreach ( $spec['processors'] as $processor ) {
190 $obj->pushProcessor( $this->getProcessor( $processor ) );
191 }
192 }
193
194 if ( isset( $spec['handlers'] ) ) {
195 foreach ( $spec['handlers'] as $handler ) {
196 $obj->pushHandler( $this->getHandler( $handler ) );
197 }
198 }
199 return $obj;
200 }
201
202
203 /**
204 * Create or return cached processor.
205 * @param string $name Processor name
206 * @return callable
207 */
208 public function getProcessor( $name ) {
209 if ( !isset( $this->singletons['processors'][$name] ) ) {
210 $spec = $this->config['processors'][$name];
211 $processor = ObjectFactory::getObjectFromSpec( $spec );
212 $this->singletons['processors'][$name] = $processor;
213 }
214 return $this->singletons['processors'][$name];
215 }
216
217
218 /**
219 * Create or return cached handler.
220 * @param string $name Processor name
221 * @return \Monolog\Handler\HandlerInterface
222 */
223 public function getHandler( $name ) {
224 if ( !isset( $this->singletons['handlers'][$name] ) ) {
225 $spec = $this->config['handlers'][$name];
226 $handler = ObjectFactory::getObjectFromSpec( $spec );
227 if ( isset( $spec['formatter'] ) ) {
228 $handler->setFormatter(
229 $this->getFormatter( $spec['formatter'] )
230 );
231 }
232 $this->singletons['handlers'][$name] = $handler;
233 }
234 return $this->singletons['handlers'][$name];
235 }
236
237
238 /**
239 * Create or return cached formatter.
240 * @param string $name Formatter name
241 * @return \Monolog\Formatter\FormatterInterface
242 */
243 public function getFormatter( $name ) {
244 if ( !isset( $this->singletons['formatters'][$name] ) ) {
245 $spec = $this->config['formatters'][$name];
246 $formatter = ObjectFactory::getObjectFromSpec( $spec );
247 $this->singletons['formatters'][$name] = $formatter;
248 }
249 return $this->singletons['formatters'][$name];
250 }
251 }