MediaWiki  master
MwlogHandler.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
24 use Monolog\Logger;
25 
37 class MwlogHandler extends SyslogUdpHandler {
38 
42  private $appprefix;
43 
47  private $hostname;
48 
59  public function __construct(
60  $appprefix,
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 ) {
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 }
$header
__construct( $appprefix, $host, $port=514, $facility=LOG_USER, $level=Logger::DEBUG, $bubble=true)
Log handler that will append the record&#39;s channel to a fixed &#39;application prefix&#39; given at constructi...
$lines
Definition: router.php:61
$line
Definition: cdb.php:59