From: Filippo Giunchedi Date: Thu, 21 Mar 2019 14:42:19 +0000 (+0100) Subject: monolog: add MwlogHandler X-Git-Tag: 1.34.0-rc.0~2199 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/modifier.php?a=commitdiff_plain;h=e783e152a2125d440ca56e119d380064f15ddf37;p=lhc%2Fweb%2Fwiklou.git monolog: add MwlogHandler Introduce a new handler, similar to SyslogHandler, that will be used to replace udp2log. The main feature of the handler is being able to vary the 'application name' with each record's channel. This way the channel can be reconstructed later, e.g. when writing to plaintext files. See also an overview of the plan here: https://phabricator.wikimedia.org/T205856#4957430 Bug: T126989 Change-Id: I0c040825b31cd70f197dc8c1d406a103dc0ed1d1 --- diff --git a/autoload.php b/autoload.php index b38c438de7..95dd2aee5e 100644 --- a/autoload.php +++ b/autoload.php @@ -900,6 +900,7 @@ $wgAutoloadLocalClasses = [ 'MediaWiki\\Logger\\Monolog\\LegacyHandler' => __DIR__ . '/includes/debug/logger/monolog/LegacyHandler.php', 'MediaWiki\\Logger\\Monolog\\LineFormatter' => __DIR__ . '/includes/debug/logger/monolog/LineFormatter.php', 'MediaWiki\\Logger\\Monolog\\LogstashFormatter' => __DIR__ . '/includes/debug/logger/monolog/LogstashFormatter.php', + 'MediaWiki\\Logger\\Monolog\\MwlogHandler' => __DIR__ . '/includes/debug/logger/monolog/MwlogHandler.php', 'MediaWiki\\Logger\\Monolog\\SyslogHandler' => __DIR__ . '/includes/debug/logger/monolog/SyslogHandler.php', 'MediaWiki\\Logger\\Monolog\\WikiProcessor' => __DIR__ . '/includes/debug/logger/monolog/WikiProcessor.php', 'MediaWiki\\Logger\\NullSpi' => __DIR__ . '/includes/debug/logger/NullSpi.php', diff --git a/includes/debug/logger/monolog/MwlogHandler.php b/includes/debug/logger/monolog/MwlogHandler.php new file mode 100644 index 0000000000..e61aadaea2 --- /dev/null +++ b/includes/debug/logger/monolog/MwlogHandler.php @@ -0,0 +1,102 @@ +appprefix = $appprefix; + $this->hostname = php_uname( 'n' ); + } + + protected function syslogHeader( $severity, $app ) { + $pri = $severity + $this->facility; + + // Goofy date format courtesy of RFC 3164 :( + // RFC 3164 actually specifies that the day of month should be space + // padded rather than unpadded but this seems to work with rsyslog and + // Logstash. + $timestamp = date( 'M j H:i:s' ); + + return "<{$pri}>{$timestamp} {$this->hostname} {$app}: "; + } + + private function splitMessageIntoLines( $message ): array { + if ( is_array( $message ) ) { + $message = implode( "\n", $message ); + } + + return preg_split( '/$\R?^/m', (string)$message, -1, PREG_SPLIT_NO_EMPTY ); + } + + protected function write( array $record ) { + $lines = $this->splitMessageIntoLines( $record['formatted'] ); + $header = $this->syslogHeader( + $this->logLevels[$record['level']], + $this->appprefix . $record['channel'] ); + + foreach ( $lines as $line ) { + $this->socket->write( $line, $header ); + } + } +}