MediaWiki master
MwlogHandler.php
Go to the documentation of this file.
1<?php
22
23use Monolog\Handler\SyslogUdpHandler;
24use Monolog\Logger;
25
47class MwlogHandler extends SyslogUdpHandler {
48
52 private $appprefix;
53
57 private $hostname;
58
69 public function __construct(
70 $appprefix,
71 $host,
72 $port = 514,
73 $facility = LOG_USER,
74 $level = Logger::DEBUG,
75 $bubble = true
76 ) {
77 parent::__construct( $host, $port, $facility, $level, $bubble );
78 $this->appprefix = $appprefix;
79 $this->hostname = php_uname( 'n' );
80 }
81
82 protected function syslogHeader( $severity, $app ) {
83 $pri = $severity + $this->facility;
84
85 // Goofy date format courtesy of RFC 3164 :(
86 // RFC 3164 actually specifies that the day of month should be space
87 // padded rather than unpadded but this seems to work with rsyslog and
88 // Logstash.
89 $timestamp = date( 'M j H:i:s' );
90
91 return "<{$pri}>{$timestamp} {$this->hostname} {$app}: ";
92 }
93
94 private function splitMessageIntoLines( $message ): array {
95 if ( is_array( $message ) ) {
96 $message = implode( "\n", $message );
97 }
98
99 return preg_split( '/$\R?^/m', (string)$message, -1, PREG_SPLIT_NO_EMPTY );
100 }
101
102 protected function write( array $record ): void {
103 $lines = $this->splitMessageIntoLines( $record['formatted'] );
104 $header = $this->syslogHeader(
105 $this->logLevels[$record['level']],
106 $this->appprefix . $record['channel'] );
107
108 foreach ( $lines as $line ) {
109 $this->socket->write( $line, $header );
110 }
111 }
112}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Write logs to syslog with the channel appended to the application name.
__construct( $appprefix, $host, $port=514, $facility=LOG_USER, $level=Logger::DEBUG, $bubble=true)
if(!file_exists( $CREDITS)) $lines
$header