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
33 * the log event. The special prefix "{channel}" will use the log event's
34 * channel as the prefix value.
36 * When not targeting a udp2log stream this class will act as a drop-in
37 * replacement for Monolog's StreamHandler.
40 * @author Bryan Davis <bd808@wikimedia.org>
41 * @copyright © 2013 Bryan Davis and Wikimedia Foundation.
43 class MWLoggerMonologHandler
extends \Monolog\Handler\AbstractProcessingHandler
{
52 * Filter log events using legacy rules
53 * @var bool $useLegacyFilter
55 protected $useLegacyFilter;
85 * @param string $stream Stream URI
86 * @param bool $useLegacyFilter Filter log events using legacy rules
87 * @param int $level Minimum logging level that will trigger handler
88 * @param bool $bubble Can handled meesages bubble up the handler stack?
90 public function __construct(
92 $useLegacyFilter = false,
93 $level = \Monolog\Logger
::DEBUG
,
96 parent
::__construct( $level, $bubble );
98 $this->useLegacyFilter
= $useLegacyFilter;
102 * Open the log sink described by our stream URI.
104 protected function openSink() {
106 throw new LogicException(
107 'Missing stream uri, the stream can not be opened.' );
110 set_error_handler( array( $this, 'errorTrap' ) );
112 if ( substr( $this->uri
, 0, 4 ) == 'udp:' ) {
113 $parsed = parse_url( $this->uri
);
114 if ( !isset( $parsed['host'] ) ) {
115 throw new UnexpectedValueException( sprintf(
116 'Udp transport "%s" must specify a host', $this->uri
119 if ( !isset( $parsed['port'] ) ) {
120 throw new UnexpectedValueException( sprintf(
121 'Udp transport "%s" must specify a port', $this->uri
125 $this->host
= $parsed['host'];
126 $this->port
= $parsed['port'];
129 if ( isset( $parsed['path'] ) ) {
130 $this->prefix
= ltrim( $parsed['path'], '/' );
133 if ( filter_var( $this->host
, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) ) {
140 $this->sink
= socket_create( $domain, SOCK_DGRAM
, SOL_UDP
);
143 $this->sink
= fopen( $this->uri
, 'a' );
145 restore_error_handler();
147 if ( !is_resource( $this->sink
) ) {
149 throw new UnexpectedValueException( sprintf(
150 'The stream or file "%s" could not be opened: %s',
151 $this->uri
, $this->error
158 * Custom error handler.
159 * @param int $code Error number
160 * @param string $msg Error message
162 protected function errorTrap( $code, $msg ) {
168 * Should we use UDP to send messages to the sink?
171 protected function useUdp() {
172 return $this->host
!== null;
176 protected function write( array $record ) {
177 if ( $this->useLegacyFilter
&&
178 !MWLoggerLegacyLogger
::shouldEmit(
179 $record['channel'], $record['message'],
180 $record['level'], $record
182 // Do not write record if we are enforcing legacy rules and they
183 // do not pass this message. This used to be done in isHandling(),
184 // but Monolog 1.12.0 made a breaking change that removed access
185 // to the needed channel and context information.
189 if ( $this->sink
=== null ) {
193 $text = (string)$record['formatted'];
194 if ( $this->useUdp() ) {
196 // Clean it up for the multiplexer
197 if ( $this->prefix
!== '' ) {
198 $leader = ( $this->prefix
=== '{channel}' ) ?
199 $record['channel'] : $this->prefix
;
200 $text = preg_replace( '/^/m', "{$leader} ", $text );
203 if ( strlen( $text ) > 65506 ) {
204 $text = substr( $text, 0, 65506 );
207 if ( substr( $text, -1 ) != "\n" ) {
211 } elseif ( strlen( $text ) > 65507 ) {
212 $text = substr( $text, 0, 65507 );
216 $this->sink
, $text, strlen( $text ), 0, $this->host
, $this->port
);
219 fwrite( $this->sink
, $text );
224 public function close() {
225 if ( is_resource( $this->sink
) ) {
226 if ( $this->useUdp() ) {
227 socket_close( $this->sink
);
230 fclose( $this->sink
);