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;
100 public function isHandling( array $record ) {
101 $levelOk = parent
::isHandling( $record );
102 if ( $levelOk && $this->useLegacyFilter
) {
103 return MWLoggerLegacyLogger
::shouldEmit(
104 $record['channel'], $record['message'], $record
111 * Open the log sink described by our stream URI.
113 protected function openSink() {
115 throw new LogicException(
116 'Missing stream uri, the stream can not be opened.' );
119 set_error_handler( array( $this, 'errorTrap' ) );
121 if ( substr( $this->uri
, 0, 4 ) == 'udp:' ) {
122 $parsed = parse_url( $this->uri
);
123 if ( !isset( $parsed['host'] ) ) {
124 throw new UnexpectedValueException( sprintf(
125 'Udp transport "%s" must specify a host', $this->uri
128 if ( !isset( $parsed['port'] ) ) {
129 throw new UnexpectedValueException( sprintf(
130 'Udp transport "%s" must specify a port', $this->uri
134 $this->host
= $parsed['host'];
135 $this->port
= $parsed['port'];
138 if ( isset( $parsed['path'] ) ) {
139 $this->prefix
= ltrim( $parsed['path'], '/' );
142 if ( filter_var( $this->host
, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) ) {
149 $this->sink
= socket_create( $domain, SOCK_DGRAM
, SOL_UDP
);
152 $this->sink
= fopen( $this->uri
, 'a' );
154 restore_error_handler();
156 if ( !is_resource( $this->sink
) ) {
158 throw new UnexpectedValueException( sprintf(
159 'The stream or file "%s" could not be opened: %s',
160 $this->uri
, $this->error
167 * Custom error handler.
168 * @param int $code Error number
169 * @param string $msg Error message
171 protected function errorTrap( $code, $msg ) {
177 * Should we use UDP to send messages to the sink?
180 protected function useUdp() {
181 return $this->host
!== null;
185 protected function write( array $record ) {
186 if ( $this->sink
=== null ) {
190 $text = (string)$record['formatted'];
191 if ( $this->useUdp() ) {
193 // Clean it up for the multiplexer
194 if ( $this->prefix
!== '' ) {
195 $text = preg_replace( '/^/m', "{$this->prefix} ", $text );
198 if ( strlen( $text ) > 65506 ) {
199 $text = substr( $text, 0, 65506 );
202 if ( substr( $text, -1 ) != "\n" ) {
206 } elseif ( strlen( $text ) > 65507 ) {
207 $text = substr( $text, 0, 65507 );
211 $this->sink
, $text, strlen( $text ), 0, $this->host
, $this->port
);
214 fwrite( $this->sink
, $text );
219 public function close() {
220 if ( is_resource( $this->sink
) ) {
221 if ( $this->useUdp() ) {
222 socket_close( $this->sink
);
225 fclose( $this->sink
);