2014-03-21 04:51:45 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2015-03-23 00:53:24 +00:00
|
|
|
namespace MediaWiki\Logger\Monolog;
|
|
|
|
|
|
|
|
|
|
use LogicException;
|
|
|
|
|
use MediaWiki\Logger\LegacyLogger;
|
|
|
|
|
use Monolog\Handler\AbstractProcessingHandler;
|
|
|
|
|
use Monolog\Logger;
|
|
|
|
|
use UnexpectedValueException;
|
2014-03-21 04:51:45 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-09-13 19:57:14 +00:00
|
|
|
* Log handler that replicates the behavior of MediaWiki's former wfErrorLog()
|
2014-03-21 04:51:45 +00:00
|
|
|
* logging service. Log output can be directed to a local file, a PHP stream,
|
|
|
|
|
* or a udp2log server.
|
|
|
|
|
*
|
|
|
|
|
* For udp2log output, the stream specification must have the form:
|
|
|
|
|
* "udp://HOST:PORT[/PREFIX]"
|
|
|
|
|
* where:
|
|
|
|
|
* - HOST: IPv4, IPv6 or hostname
|
|
|
|
|
* - PORT: server port
|
|
|
|
|
* - PREFIX: optional (but recommended) prefix telling udp2log how to route
|
2015-03-22 22:12:25 +00:00
|
|
|
* the log event. The special prefix "{channel}" will use the log event's
|
|
|
|
|
* channel as the prefix value.
|
2014-03-21 04:51:45 +00:00
|
|
|
*
|
|
|
|
|
* When not targeting a udp2log stream this class will act as a drop-in
|
2015-11-23 22:53:38 +00:00
|
|
|
* replacement for \Monolog\Handler\StreamHandler.
|
2014-03-21 04:51:45 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.25
|
2017-06-13 16:51:53 +00:00
|
|
|
* @copyright © 2013 Wikimedia Foundation and contributors
|
2014-03-21 04:51:45 +00:00
|
|
|
*/
|
2015-03-23 00:53:24 +00:00
|
|
|
class LegacyHandler extends AbstractProcessingHandler {
|
2014-03-21 04:51:45 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log sink descriptor
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2014-03-21 04:51:45 +00:00
|
|
|
*/
|
|
|
|
|
protected $uri;
|
|
|
|
|
|
2014-11-21 06:23:49 +00:00
|
|
|
/**
|
|
|
|
|
* Filter log events using legacy rules
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var bool
|
2014-11-21 06:23:49 +00:00
|
|
|
*/
|
|
|
|
|
protected $useLegacyFilter;
|
|
|
|
|
|
2014-03-21 04:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* Log sink
|
2021-09-22 20:16:51 +00:00
|
|
|
* @var resource|null
|
2014-03-21 04:51:45 +00:00
|
|
|
*/
|
|
|
|
|
protected $sink;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2014-03-21 04:51:45 +00:00
|
|
|
*/
|
|
|
|
|
protected $error;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2014-03-21 04:51:45 +00:00
|
|
|
*/
|
|
|
|
|
protected $host;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var int
|
2014-03-21 04:51:45 +00:00
|
|
|
*/
|
|
|
|
|
protected $port;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2014-03-21 04:51:45 +00:00
|
|
|
*/
|
|
|
|
|
protected $prefix;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $stream Stream URI
|
2014-11-21 06:23:49 +00:00
|
|
|
* @param bool $useLegacyFilter Filter log events using legacy rules
|
2014-03-21 04:51:45 +00:00
|
|
|
* @param int $level Minimum logging level that will trigger handler
|
|
|
|
|
* @param bool $bubble Can handled meesages bubble up the handler stack?
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2014-11-21 06:23:49 +00:00
|
|
|
$stream,
|
|
|
|
|
$useLegacyFilter = false,
|
2015-03-23 00:53:24 +00:00
|
|
|
$level = Logger::DEBUG,
|
2014-11-21 06:23:49 +00:00
|
|
|
$bubble = true
|
2014-03-21 04:51:45 +00:00
|
|
|
) {
|
|
|
|
|
parent::__construct( $level, $bubble );
|
|
|
|
|
$this->uri = $stream;
|
2014-11-21 06:23:49 +00:00
|
|
|
$this->useLegacyFilter = $useLegacyFilter;
|
2014-03-21 04:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open the log sink described by our stream URI.
|
|
|
|
|
*/
|
|
|
|
|
protected function openSink() {
|
|
|
|
|
if ( !$this->uri ) {
|
|
|
|
|
throw new LogicException(
|
|
|
|
|
'Missing stream uri, the stream can not be opened.' );
|
|
|
|
|
}
|
|
|
|
|
$this->error = null;
|
2016-02-17 09:09:32 +00:00
|
|
|
set_error_handler( [ $this, 'errorTrap' ] );
|
2014-03-21 04:51:45 +00:00
|
|
|
|
|
|
|
|
if ( substr( $this->uri, 0, 4 ) == 'udp:' ) {
|
|
|
|
|
$parsed = parse_url( $this->uri );
|
|
|
|
|
if ( !isset( $parsed['host'] ) ) {
|
|
|
|
|
throw new UnexpectedValueException( sprintf(
|
|
|
|
|
'Udp transport "%s" must specify a host', $this->uri
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $parsed['port'] ) ) {
|
|
|
|
|
throw new UnexpectedValueException( sprintf(
|
|
|
|
|
'Udp transport "%s" must specify a port', $this->uri
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->host = $parsed['host'];
|
|
|
|
|
$this->port = $parsed['port'];
|
|
|
|
|
$this->prefix = '';
|
|
|
|
|
|
|
|
|
|
if ( isset( $parsed['path'] ) ) {
|
|
|
|
|
$this->prefix = ltrim( $parsed['path'], '/' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
|
|
|
|
|
$domain = AF_INET6;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
$domain = AF_INET;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
$this->sink = fopen( $this->uri, 'a' );
|
|
|
|
|
}
|
|
|
|
|
restore_error_handler();
|
|
|
|
|
|
|
|
|
|
if ( !is_resource( $this->sink ) ) {
|
|
|
|
|
$this->sink = null;
|
|
|
|
|
throw new UnexpectedValueException( sprintf(
|
|
|
|
|
'The stream or file "%s" could not be opened: %s',
|
|
|
|
|
$this->uri, $this->error
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Custom error handler.
|
|
|
|
|
* @param int $code Error number
|
|
|
|
|
* @param string $msg Error message
|
|
|
|
|
*/
|
|
|
|
|
protected function errorTrap( $code, $msg ) {
|
|
|
|
|
$this->error = $msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Should we use UDP to send messages to the sink?
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function useUdp() {
|
|
|
|
|
return $this->host !== null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-14 00:03:34 +00:00
|
|
|
protected function write( array $record ): void {
|
2015-02-14 01:08:21 +00:00
|
|
|
if ( $this->useLegacyFilter &&
|
2015-03-23 00:53:24 +00:00
|
|
|
!LegacyLogger::shouldEmit(
|
2015-02-14 01:08:21 +00:00
|
|
|
$record['channel'], $record['message'],
|
|
|
|
|
$record['level'], $record
|
|
|
|
|
) ) {
|
|
|
|
|
// Do not write record if we are enforcing legacy rules and they
|
|
|
|
|
// do not pass this message. This used to be done in isHandling(),
|
|
|
|
|
// but Monolog 1.12.0 made a breaking change that removed access
|
|
|
|
|
// to the needed channel and context information.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-21 04:51:45 +00:00
|
|
|
if ( $this->sink === null ) {
|
|
|
|
|
$this->openSink();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-30 16:50:19 +00:00
|
|
|
$text = (string)$record['formatted'];
|
2014-03-21 04:51:45 +00:00
|
|
|
if ( $this->useUdp() ) {
|
|
|
|
|
// Clean it up for the multiplexer
|
|
|
|
|
if ( $this->prefix !== '' ) {
|
2015-03-22 22:12:25 +00:00
|
|
|
$leader = ( $this->prefix === '{channel}' ) ?
|
|
|
|
|
$record['channel'] : $this->prefix;
|
|
|
|
|
$text = preg_replace( '/^/m', "{$leader} ", $text );
|
2014-03-21 04:51:45 +00:00
|
|
|
|
2019-09-09 08:49:23 +00:00
|
|
|
// Limit to 64 KiB
|
2014-03-21 04:51:45 +00:00
|
|
|
if ( strlen( $text ) > 65506 ) {
|
|
|
|
|
$text = substr( $text, 0, 65506 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( substr( $text, -1 ) != "\n" ) {
|
|
|
|
|
$text .= "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} elseif ( strlen( $text ) > 65507 ) {
|
|
|
|
|
$text = substr( $text, 0, 65507 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socket_sendto(
|
2015-03-23 00:53:24 +00:00
|
|
|
$this->sink, $text, strlen( $text ), 0, $this->host, $this->port
|
|
|
|
|
);
|
2014-03-21 04:51:45 +00:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
fwrite( $this->sink, $text );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-14 00:03:34 +00:00
|
|
|
public function close(): void {
|
2014-03-21 04:51:45 +00:00
|
|
|
if ( is_resource( $this->sink ) ) {
|
|
|
|
|
if ( $this->useUdp() ) {
|
|
|
|
|
socket_close( $this->sink );
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
fclose( $this->sink );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->sink = null;
|
|
|
|
|
}
|
|
|
|
|
}
|