ff6336bd00d8fe986df278257df3045d34f6ab28
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 logger that mimics the historic implementation of MediaWiki's
24 * wfErrorLog logging implementation.
26 * This logger is configured by the following global configuration variables:
28 * - `$wgDebugLogGroups`
32 * See documentation in DefaultSettings.php for detailed explanations of each
37 * @author Bryan Davis <bd808@wikimedia.org>
38 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
40 class MWLoggerLegacyLogger
extends \Psr\Log\AbstractLogger
{
43 * @var string $channel
49 * @param string $channel
51 public function __construct( $channel ) {
52 $this->channel
= $channel;
56 * Logs with an arbitrary level.
58 * @param string|int $level
59 * @param string $message
60 * @param array $context
62 public function log( $level, $message, array $context = array() ) {
63 if ( self
::shouldEmit( $this->channel
, $message, $context ) ) {
64 $text = self
::format( $this->channel
, $message, $context );
65 $destination = self
::destination( $this->channel
, $message, $context );
66 self
::emit( $text, $destination );
72 * Determine if the given message should be emitted or not.
74 * @param string $channel
75 * @param string $message
76 * @param array $context
77 * @return bool True if message should be sent to disk/network, false
80 protected static function shouldEmit( $channel, $message, $context ) {
81 global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups;
83 if ( $channel === 'wfLogDBError' ) {
84 // wfLogDBError messages are emitted if a database log location is
86 $shouldEmit = (bool) $wgDBerrorLog;
88 } elseif ( $channel === 'wfErrorLog' ) {
89 // All messages on the wfErrorLog channel should be emitted.
92 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
93 $logConfig = $wgDebugLogGroups[$channel];
95 if ( is_array( $logConfig ) && isset( $logConfig['sample'] ) ) {
96 // Emit randomly with a 1 in 'sample' chance for each message.
97 $shouldEmit = mt_rand( 1, $logConfig['sample'] ) === 1;
100 // Emit unless the config value is explictly false.
101 $shouldEmit = $logConfig !== false;
104 } elseif ( isset( $context['private'] ) && $context['private'] ) {
105 // Don't emit if the message didn't match previous checks based on the
106 // channel and the event is marked as private. This check discards
107 // messages sent via wfDebugLog() with dest == 'private' and no explicit
108 // wgDebugLogGroups configuration.
111 // Default return value is the the same as the historic wfDebug
112 // method: emit if $wgDebugLogFile has been set.
113 $shouldEmit = $wgDebugLogFile != '';
123 * Messages to the 'wfDebug', 'wfLogDBError' and 'wfErrorLog' channels
124 * receive special fomatting to mimic the historic output of the functions
125 * of the same name. All other channel values are formatted based on the
126 * historic output of the `wfDebugLog()` global function.
128 * @param string $channel
129 * @param string $message
130 * @param array $context
133 protected static function format( $channel, $message, $context ) {
134 global $wgDebugLogGroups;
136 if ( $channel === 'wfDebug' ) {
137 $text = self
::formatWfDebug( $channel, $message, $context );
139 } elseif ( $channel === 'wfLogDBError' ) {
140 $text = self
::formatWfLogDBError( $channel, $message, $context );
142 } elseif ( $channel === 'wfErrorLog' ) {
143 $text = "{$message}\n";
145 } elseif ( !isset( $wgDebugLogGroups[$channel] ) ) {
146 $text = self
::formatWfDebug(
147 $channel, "[{$channel}] {$message}", $context );
150 // Default formatting is wfDebugLog's historic style
151 $time = wfTimestamp( TS_DB
);
153 $host = wfHostname();
154 $text = "{$time} {$host} {$wiki}: {$message}\n";
161 * Format a message as `wfDebug()` would have formatted it.
163 * @param string $channel
164 * @param string $message
165 * @param array $context
168 protected static function formatWfDebug( $channel, $message, $context ) {
169 $text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $message );
170 if ( isset( $context['prefix'] ) ) {
171 $text = "{$context['prefix']}{$text}";
178 * Format a message as `wfLogDBError()` would have formatted it.
180 * @param string $channel
181 * @param string $message
182 * @param array $context
185 protected static function formatWfLogDBError( $channel, $message, $context ) {
186 global $wgDBerrorLogTZ;
187 static $cachedTimezone = null;
189 if ( $wgDBerrorLogTZ && !$cachedTimezone ) {
190 $cachedTimezone = new DateTimeZone( $wgDBerrorLogTZ );
193 // Workaround for https://bugs.php.net/bug.php?id=52063
194 // Can be removed when min PHP > 5.3.2
195 if ( $cachedTimezone === null ) {
196 $d = date_create( 'now' );
198 $d = date_create( 'now', $cachedTimezone );
200 $date = $d->format( 'D M j G:i:s T Y' );
202 $host = wfHostname();
205 $text = "{$date}\t{$host}\t{$wiki}\t{$message}\n";
211 * Select the appropriate log output destination for the given log event.
213 * If the event context contains 'destination'
215 * @param string $channel
216 * @param string $message
217 * @param array $context
220 protected static function destination( $channel, $message, $context ) {
221 global $wgDebugLogFile, $wgDBerrorLog, $wgDebugLogGroups;
223 // Default destination is the debug log file as historically used by
224 // the wfDebug function.
225 $destination = $wgDebugLogFile;
227 if ( isset( $context['destination'] ) ) {
228 // Use destination explicitly provided in context
229 $destination = $context['destination'];
231 } elseif ( $channel === 'wfDebug' ) {
232 $destination = $wgDebugLogFile;
234 } elseif ( $channel === 'wfLogDBError' ) {
235 $destination = $wgDBerrorLog;
237 } elseif ( isset( $wgDebugLogGroups[$channel] ) ) {
238 $logConfig = $wgDebugLogGroups[$channel];
240 if ( is_array( $logConfig ) ) {
241 $destination = $logConfig['destination'];
243 $destination = strval( $logConfig );
252 * Log to a file without getting "file size exceeded" signals.
254 * Can also log to UDP with the syntax udp://host:port/prefix. This will send
255 * lines to the specified port, prefixed by the specified prefix and a space.
257 * @param string $text
258 * @param string $file Filename
259 * @throws MWException
261 public static function emit( $text, $file ) {
262 if ( substr( $file, 0, 4 ) == 'udp:' ) {
263 # Needs the sockets extension
264 if ( preg_match( '!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
265 // IPv6 bracketed host
267 $port = intval( $m[2] );
268 $prefix = isset( $m[3] ) ?
$m[3] : false;
270 } elseif ( preg_match( '!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
272 if ( !IP
::isIPv4( $host ) ) {
273 $host = gethostbyname( $host );
275 $port = intval( $m[2] );
276 $prefix = isset( $m[3] ) ?
$m[3] : false;
279 throw new MWException( __METHOD__
. ': Invalid UDP specification' );
282 // Clean it up for the multiplexer
283 if ( strval( $prefix ) !== '' ) {
284 $text = preg_replace( '/^/m', $prefix . ' ', $text );
287 if ( strlen( $text ) > 65506 ) {
288 $text = substr( $text, 0, 65506 );
291 if ( substr( $text, -1 ) != "\n" ) {
294 } elseif ( strlen( $text ) > 65507 ) {
295 $text = substr( $text, 0, 65507 );
298 $sock = socket_create( $domain, SOCK_DGRAM
, SOL_UDP
);
303 socket_sendto( $sock, $text, strlen( $text ), 0, $host, $port );
304 socket_close( $sock );
306 wfSuppressWarnings();
307 $exists = file_exists( $file );
308 $size = $exists ?
filesize( $file ) : false;
310 ( $size !== false && $size +
strlen( $text ) < 0x7fffffff )
312 file_put_contents( $file, $text, FILE_APPEND
);