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
24 * Log handler that replicates the behavior of MediaWiki's wfErrorLog()
25 * logging service. Log output can be directed to a local file, a PHP stream,
26 * or a udp2log server.
28 * For udp2log output, the stream specification must have the form:
29 * "udp://HOST:PORT[/PREFIX]"
31 * - HOST: IPv4, IPv6 or hostname
33 * - PREFIX: optional (but recommended) prefix telling udp2log how to route
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
{
79 * @param string $stream Stream URI
80 * @param int $level Minimum logging level that will trigger handler
81 * @param bool $bubble Can handled meesages bubble up the handler stack?
83 public function __construct(
84 $stream, $level = \Monolog\Logger
::DEBUG
, $bubble = true
86 parent
::__construct( $level, $bubble );
92 * Open the log sink described by our stream URI.
94 protected function openSink() {
96 throw new LogicException(
97 'Missing stream uri, the stream can not be opened.' );
100 set_error_handler( array( $this, 'errorTrap' ) );
102 if ( substr( $this->uri
, 0, 4 ) == 'udp:' ) {
103 $parsed = parse_url( $this->uri
);
104 if ( !isset( $parsed['host'] ) ) {
105 throw new UnexpectedValueException( sprintf(
106 'Udp transport "%s" must specify a host', $this->uri
109 if ( !isset( $parsed['port'] ) ) {
110 throw new UnexpectedValueException( sprintf(
111 'Udp transport "%s" must specify a port', $this->uri
115 $this->host
= $parsed['host'];
116 $this->port
= $parsed['port'];
119 if ( isset( $parsed['path'] ) ) {
120 $this->prefix
= ltrim( $parsed['path'], '/' );
123 if ( filter_var( $this->host
, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) ) {
130 $this->sink
= socket_create( $domain, SOCK_DGRAM
, SOL_UDP
);
133 $this->sink
= fopen( $this->uri
, 'a' );
135 restore_error_handler();
137 if ( !is_resource( $this->sink
) ) {
139 throw new UnexpectedValueException( sprintf(
140 'The stream or file "%s" could not be opened: %s',
141 $this->uri
, $this->error
148 * Custom error handler.
149 * @param int $code Error number
150 * @param string $msg Error message
152 protected function errorTrap( $code, $msg ) {
158 * Should we use UDP to send messages to the sink?
161 protected function useUdp() {
162 return $this->host
!== null;
166 protected function write( array $record ) {
167 if ( $this->sink
=== null ) {
171 $text = (string)$record['formatted'];
172 if ( $this->useUdp() ) {
174 // Clean it up for the multiplexer
175 if ( $this->prefix
!== '' ) {
176 $text = preg_replace( '/^/m', "{$this->prefix} ", $text );
179 if ( strlen( $text ) > 65506 ) {
180 $text = substr( $text, 0, 65506 );
183 if ( substr( $text, -1 ) != "\n" ) {
187 } elseif ( strlen( $text ) > 65507 ) {
188 $text = substr( $text, 0, 65507 );
192 $this->sink
, $text, strlen( $text ), 0, $this->host
, $this->port
);
195 fwrite( $this->sink
, $text );
200 public function close() {
201 if ( is_resource( $this->sink
) ) {
202 if ( $this->useUdp() ) {
203 socket_close( $this->sink
);
206 fclose( $this->sink
);