Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SyslogHandler
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 makeCommonSyslogHeader
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Logger\Monolog;
8
9use DateTimeInterface;
10use Monolog\Handler\SyslogUdpHandler;
11use Monolog\Level;
12
13/**
14 * Write logs to a syslog server, using RFC 3164 formatted UDP packets.
15 *
16 * This builds on Monolog's SyslogUdpHandler, which creates only a partial
17 * RFC 5424 header (PRI and VERSION), with rest intending to come
18 * from a specifically configured LineFormatter.
19 *
20 * This makes use of SyslogUdpHandler it impossible with a formatter like
21 * \Monolog\Formatter\LogstashFormatter. Additionally, the direct syslog
22 * input for Logstash requires and accepts only RFC 3164 formatted packets.
23 *
24 * This is a complete syslog handler and should work with any formatter. The
25 * formatted message will be prepended with a complete RFC 3164 message
26 * header and a partial message body. The resulting packet looks like:
27 *
28 *   <PRI>DATETIME HOSTNAME PROGRAM: MESSAGE
29 *
30 * This format works as input to rsyslog and can also be processed by the
31 * default Logstash syslog input handler.
32 *
33 * @since 1.25
34 * @ingroup Debug
35 * @copyright © 2015 Wikimedia Foundation and contributors
36 */
37class SyslogHandler extends SyslogUdpHandler {
38
39    private string $appname;
40    private string $hostname;
41
42    /**
43     * @param string $appname Application name to report to syslog
44     * @param string $host Syslog host
45     * @param int $port Syslog port
46     * @param int $facility Syslog message facility
47     * @param int|string|Level $level The minimum logging level at which this
48     *   handler will be triggered
49     * @param bool $bubble Whether the messages that are handled can bubble up
50     *   the stack or not
51     */
52    public function __construct(
53        $appname,
54        $host,
55        $port = 514,
56        $facility = LOG_USER,
57        $level = Level::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}