MediaWiki master
SyslogHandler.php
Go to the documentation of this file.
1<?php
8
9use DateTimeInterface;
10use Monolog\Handler\SyslogUdpHandler;
11use Monolog\Logger;
12
37class SyslogHandler extends SyslogUdpHandler {
38
39 private string $appname;
40 private string $hostname;
41
52 public function __construct(
53 $appname,
54 $host,
55 $port = 514,
56 $facility = LOG_USER,
57 $level = Logger::DEBUG,
58 $bubble = true
59 ) {
60 parent::__construct( $host, $port, $facility, $level, $bubble );
61 $this->appname = $appname;
62 $this->hostname = php_uname( 'n' );
63 }
64
65 protected function makeCommonSyslogHeader( int $severity, DateTimeInterface $datetime ): string {
66 $pri = $severity + $this->facility;
67
68 // Goofy date format courtesy of RFC 3164 :(
69 // RFC 3164 actually specifies that the day of month should be space
70 // padded rather than unpadded but this seems to work with rsyslog and
71 // Logstash.
72 $timestamp = date( 'M j H:i:s' );
73
74 return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: ";
75 }
76}
Write logs to a syslog server, using RFC 3164 formatted UDP packets.
makeCommonSyslogHeader(int $severity, DateTimeInterface $datetime)
__construct( $appname, $host, $port=514, $facility=LOG_USER, $level=Logger::DEBUG, $bubble=true)