MediaWiki REL1_35
MwlogHandler.php
Go to the documentation of this file.
1<?php
22
23use Monolog\Handler\SyslogUdpHandler;
24use Monolog\Logger;
25
37class MwlogHandler extends SyslogUdpHandler {
38
42 private $appprefix;
43
47 private $hostname;
48
59 public function __construct(
61 $host,
62 $port = 514,
63 $facility = LOG_USER,
64 $level = Logger::DEBUG,
65 $bubble = true
66 ) {
67 parent::__construct( $host, $port, $facility, $level, $bubble );
68 $this->appprefix = $appprefix;
69 $this->hostname = php_uname( 'n' );
70 }
71
72 protected function syslogHeader( $severity, $app ) {
73 $pri = $severity + $this->facility;
74
75 // Goofy date format courtesy of RFC 3164 :(
76 // RFC 3164 actually specifies that the day of month should be space
77 // padded rather than unpadded but this seems to work with rsyslog and
78 // Logstash.
79 $timestamp = date( 'M j H:i:s' );
80
81 return "<{$pri}>{$timestamp} {$this->hostname} {$app}: ";
82 }
83
84 private function splitMessageIntoLines( $message ): array {
85 if ( is_array( $message ) ) {
86 $message = implode( "\n", $message );
87 }
88
89 return preg_split( '/$\R?^/m', (string)$message, -1, PREG_SPLIT_NO_EMPTY );
90 }
91
92 protected function write( array $record ): void {
93 $lines = $this->splitMessageIntoLines( $record['formatted'] );
94 $header = $this->syslogHeader(
95 $this->logLevels[$record['level']],
96 $this->appprefix . $record['channel'] );
97
98 foreach ( $lines as $line ) {
99 $this->socket->write( $line, $header );
100 }
101 }
102}
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
Log handler that will append the record's channel to a fixed 'application prefix' given at constructi...
__construct( $appprefix, $host, $port=514, $facility=LOG_USER, $level=Logger::DEBUG, $bubble=true)
$line
Definition mcc.php:119
if(!file_exists( $CREDITS)) $lines
$header