SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / debug / logger / monolog / Handler.php
bloba872d84e28e7986f084efd6fc9dfdc4a2731c062
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.
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
18 * @file
22 /**
23 * Log handler that replicates the behavior of MediaWiki's wfErrorLog()
24 * logging service. Log output can be directed to a local file, a PHP stream,
25 * or a udp2log server.
27 * For udp2log output, the stream specification must have the form:
28 * "udp://HOST:PORT[/PREFIX]"
29 * where:
30 * - HOST: IPv4, IPv6 or hostname
31 * - PORT: server port
32 * - PREFIX: optional (but recommended) prefix telling udp2log how to route
33 * the log event
35 * When not targeting a udp2log stream this class will act as a drop-in
36 * replacement for Monolog's StreamHandler.
38 * @since 1.25
39 * @author Bryan Davis <bd808@wikimedia.org>
40 * @copyright © 2013 Bryan Davis and Wikimedia Foundation.
42 class MWLoggerMonologHandler extends \Monolog\Handler\AbstractProcessingHandler {
44 /**
45 * Log sink descriptor
46 * @var string $uri
48 protected $uri;
50 /**
51 * Filter log events using legacy rules
52 * @var bool $useLegacyFilter
54 protected $useLegacyFilter;
56 /**
57 * Log sink
58 * @var resource $sink
60 protected $sink;
62 /**
63 * @var string $error
65 protected $error;
67 /**
68 * @var string $host
70 protected $host;
72 /**
73 * @var int $port
75 protected $port;
77 /**
78 * @var string $prefix
80 protected $prefix;
83 /**
84 * @param string $stream Stream URI
85 * @param bool $useLegacyFilter Filter log events using legacy rules
86 * @param int $level Minimum logging level that will trigger handler
87 * @param bool $bubble Can handled meesages bubble up the handler stack?
89 public function __construct(
90 $stream,
91 $useLegacyFilter = false,
92 $level = \Monolog\Logger::DEBUG,
93 $bubble = true
94 ) {
95 parent::__construct( $level, $bubble );
96 $this->uri = $stream;
97 $this->useLegacyFilter = $useLegacyFilter;
101 * Open the log sink described by our stream URI.
103 protected function openSink() {
104 if ( !$this->uri ) {
105 throw new LogicException(
106 'Missing stream uri, the stream can not be opened.' );
108 $this->error = null;
109 set_error_handler( array( $this, 'errorTrap' ) );
111 if ( substr( $this->uri, 0, 4 ) == 'udp:' ) {
112 $parsed = parse_url( $this->uri );
113 if ( !isset( $parsed['host'] ) ) {
114 throw new UnexpectedValueException( sprintf(
115 'Udp transport "%s" must specify a host', $this->uri
116 ) );
118 if ( !isset( $parsed['port'] ) ) {
119 throw new UnexpectedValueException( sprintf(
120 'Udp transport "%s" must specify a port', $this->uri
121 ) );
124 $this->host = $parsed['host'];
125 $this->port = $parsed['port'];
126 $this->prefix = '';
128 if ( isset( $parsed['path'] ) ) {
129 $this->prefix = ltrim( $parsed['path'], '/' );
132 if ( filter_var( $this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
133 $domain = AF_INET6;
135 } else {
136 $domain = AF_INET;
139 $this->sink = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
141 } else {
142 $this->sink = fopen( $this->uri, 'a' );
144 restore_error_handler();
146 if ( !is_resource( $this->sink ) ) {
147 $this->sink = null;
148 throw new UnexpectedValueException( sprintf(
149 'The stream or file "%s" could not be opened: %s',
150 $this->uri, $this->error
151 ) );
157 * Custom error handler.
158 * @param int $code Error number
159 * @param string $msg Error message
161 protected function errorTrap( $code, $msg ) {
162 $this->error = $msg;
167 * Should we use UDP to send messages to the sink?
168 * @return bool
170 protected function useUdp() {
171 return $this->host !== null;
175 protected function write( array $record ) {
176 if ( $this->useLegacyFilter &&
177 !MWLoggerLegacyLogger::shouldEmit(
178 $record['channel'], $record['message'],
179 $record['level'], $record
180 ) ) {
181 // Do not write record if we are enforcing legacy rules and they
182 // do not pass this message. This used to be done in isHandling(),
183 // but Monolog 1.12.0 made a breaking change that removed access
184 // to the needed channel and context information.
185 return;
188 if ( $this->sink === null ) {
189 $this->openSink();
192 $text = (string)$record['formatted'];
193 if ( $this->useUdp() ) {
195 // Clean it up for the multiplexer
196 if ( $this->prefix !== '' ) {
197 $text = preg_replace( '/^/m', "{$this->prefix} ", $text );
199 // Limit to 64KB
200 if ( strlen( $text ) > 65506 ) {
201 $text = substr( $text, 0, 65506 );
204 if ( substr( $text, -1 ) != "\n" ) {
205 $text .= "\n";
208 } elseif ( strlen( $text ) > 65507 ) {
209 $text = substr( $text, 0, 65507 );
212 socket_sendto(
213 $this->sink, $text, strlen( $text ), 0, $this->host, $this->port );
215 } else {
216 fwrite( $this->sink, $text );
221 public function close() {
222 if ( is_resource( $this->sink ) ) {
223 if ( $this->useUdp() ) {
224 socket_close( $this->sink );
226 } else {
227 fclose( $this->sink );
230 $this->sink = null;