MediaWiki master
SyslogHandler.php
Go to the documentation of this file.
1<?php
22
23use DateTimeInterface;
24use Monolog\Handler\SyslogUdpHandler;
25use Monolog\Logger;
26
51class SyslogHandler extends SyslogUdpHandler {
52
53 private string $appname;
54 private string $hostname;
55
66 public function __construct(
67 $appname,
68 $host,
69 $port = 514,
70 $facility = LOG_USER,
71 $level = Logger::DEBUG,
72 $bubble = true
73 ) {
74 parent::__construct( $host, $port, $facility, $level, $bubble );
75 $this->appname = $appname;
76 $this->hostname = php_uname( 'n' );
77 }
78
79 protected function makeCommonSyslogHeader( int $severity, DateTimeInterface $datetime ): string {
80 $pri = $severity + $this->facility;
81
82 // Goofy date format courtesy of RFC 3164 :(
83 // RFC 3164 actually specifies that the day of month should be space
84 // padded rather than unpadded but this seems to work with rsyslog and
85 // Logstash.
86 $timestamp = date( 'M j H:i:s' );
87
88 return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: ";
89 }
90}
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)