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
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]"
30 * - HOST: IPv4, IPv6 or hostname
32 * - PREFIX: optional (but recommended) prefix telling udp2log how to route
35 * When not targeting a udp2log stream this class will act as a drop-in
36 * replacement for Monolog's StreamHandler.
39 * @author Bryan Davis <bd808@wikimedia.org>
40 * @copyright © 2013 Bryan Davis and Wikimedia Foundation.
42 class MWLoggerMonologHandler
extends \Monolog\Handler\AbstractProcessingHandler
{
51 * Filter log events using legacy rules
52 * @var bool $useLegacyFilter
54 protected $useLegacyFilter;
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(
91 $useLegacyFilter = false,
92 $level = \Monolog\Logger
::DEBUG
,
95 parent
::__construct( $level, $bubble );
97 $this->useLegacyFilter
= $useLegacyFilter;
101 * Open the log sink described by our stream URI.
103 protected function openSink() {
105 throw new LogicException(
106 'Missing stream uri, the stream can not be opened.' );
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
118 if ( !isset( $parsed['port'] ) ) {
119 throw new UnexpectedValueException( sprintf(
120 'Udp transport "%s" must specify a port', $this->uri
124 $this->host
= $parsed['host'];
125 $this->port
= $parsed['port'];
128 if ( isset( $parsed['path'] ) ) {
129 $this->prefix
= ltrim( $parsed['path'], '/' );
132 if ( filter_var( $this->host
, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) ) {
139 $this->sink
= socket_create( $domain, SOCK_DGRAM
, SOL_UDP
);
142 $this->sink
= fopen( $this->uri
, 'a' );
144 restore_error_handler();
146 if ( !is_resource( $this->sink
) ) {
148 throw new UnexpectedValueException( sprintf(
149 'The stream or file "%s" could not be opened: %s',
150 $this->uri
, $this->error
157 * Custom error handler.
158 * @param int $code Error number
159 * @param string $msg Error message
161 protected function errorTrap( $code, $msg ) {
167 * Should we use UDP to send messages to the sink?
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
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.
188 if ( $this->sink
=== null ) {
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 );
200 if ( strlen( $text ) > 65506 ) {
201 $text = substr( $text, 0, 65506 );
204 if ( substr( $text, -1 ) != "\n" ) {
208 } elseif ( strlen( $text ) > 65507 ) {
209 $text = substr( $text, 0, 65507 );
213 $this->sink
, $text, strlen( $text ), 0, $this->host
, $this->port
);
216 fwrite( $this->sink
, $text );
221 public function close() {
222 if ( is_resource( $this->sink
) ) {
223 if ( $this->useUdp() ) {
224 socket_close( $this->sink
);
227 fclose( $this->sink
);