MediaWiki  master
MwlogHandler.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Logger\Monolog;
22 
23 use Monolog\Handler\SyslogUdpHandler;
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 }
MediaWiki\Logger\Monolog\MwlogHandler
Log handler that will append the record's channel to a fixed 'application prefix' given at constructi...
Definition: MwlogHandler.php:37
MediaWiki\Logger\Monolog
Definition: AvroFormatter.php:21
MediaWiki\Logger\Monolog\MwlogHandler\write
write(array $record)
Definition: MwlogHandler.php:92
MediaWiki\Logger\Monolog\MwlogHandler\syslogHeader
syslogHeader( $severity, $app)
Definition: MwlogHandler.php:72
MediaWiki\Logger\Monolog\MwlogHandler\__construct
__construct( $appprefix, $host, $port=514, $facility=LOG_USER, $level=Logger::DEBUG, $bubble=true)
Definition: MwlogHandler.php:59
MediaWiki\Logger\Monolog\MwlogHandler\$appprefix
string $appprefix
Definition: MwlogHandler.php:42
$header
$header
Definition: updateCredits.php:41
$line
$line
Definition: mcc.php:119
$lines
if(!file_exists( $CREDITS)) $lines
Definition: updateCredits.php:49
MediaWiki\Logger\Monolog\MwlogHandler\splitMessageIntoLines
splitMessageIntoLines( $message)
Definition: MwlogHandler.php:84
MediaWiki\Logger\Monolog\MwlogHandler\$hostname
string $hostname
Definition: MwlogHandler.php:47