3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Logger\Monolog
;
23 use DateTimeInterface
;
24 use Monolog\Handler\SyslogUdpHandler
;
28 * Write logs to a syslog server, using RFC 3164 formatted UDP packets.
30 * This builds on Monolog's SyslogUdpHandler, which creates only a partial
31 * RFC 5424 header (PRI and VERSION), with rest intending to come
32 * from a specifically configured LineFormatter.
34 * This makes use of SyslogUdpHandler it impossible with a formatter like
35 * \Monolog\Formatter\LogstashFormatter. Additionally, the direct syslog
36 * input for Logstash requires and accepts only RFC 3164 formatted packets.
38 * This is a complete syslog handler and should work with any formatter. The
39 * formatted message will be prepended with a complete RFC 3164 message
40 * header and a partial message body. The resulting packet looks like:
42 * <PRI>DATETIME HOSTNAME PROGRAM: MESSAGE
44 * This format works as input to rsyslog and can also be processed by the
45 * default Logstash syslog input handler.
49 * @copyright © 2015 Wikimedia Foundation and contributors
51 class SyslogHandler
extends SyslogUdpHandler
{
53 private string $appname;
54 private string $hostname;
57 * @param string $appname Application name to report to syslog
58 * @param string $host Syslog host
59 * @param int $port Syslog port
60 * @param int $facility Syslog message facility
61 * @param int $level The minimum logging level at which this handler
63 * @param bool $bubble Whether the messages that are handled can bubble up
66 public function __construct(
71 $level = Logger
::DEBUG
,
74 parent
::__construct( $host, $port, $facility, $level, $bubble );
75 $this->appname
= $appname;
76 $this->hostname
= php_uname( 'n' );
79 protected function makeCommonSyslogHeader( int $severity, DateTimeInterface
$datetime ): string {
80 $pri = $severity +
$this->facility
;
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
86 $timestamp = date( 'M j H:i:s' );
88 return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: ";