Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MwlogHandler
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
6.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 syslogHeader
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 splitMessageIntoLines
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 write
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Logger\Monolog;
8
9use Monolog\Handler\SyslogUdpHandler;
10use Monolog\Level;
11use Monolog\LogRecord;
12
13/**
14 * Write logs to syslog with the channel appended to the application name.
15 *
16 * This use case for this handler is to emulate Wikimedia Foundation's
17 * udp2log system by leveraging syslog (and e.g. Rsyslog/Kafka) and
18 * allow an unstructured string to pass through mostly as-is, with the
19 * exception of the channel name, which is encoded in transit as part
20 * of the syslog "application name". It is intended that the syslog
21 * consumer "wildcard" subscribes to all messages with the app prefix,
22 * and then * strips it off at some point before writing the messages
23 * to a log file named after the channel.
24 *
25 * Transition plan (2016):
26 * - https://phabricator.wikimedia.org/T205856#4957430
27 * - https://phabricator.wikimedia.org/T126989
28 *
29 * @unstable
30 * @since 1.32
31 * @ingroup Debug
32 * @copyright © 2019 Wikimedia Foundation and contributors
33 */
34class MwlogHandler extends SyslogUdpHandler {
35
36    /**
37     * @var string
38     */
39    private $appprefix;
40
41    /**
42     * @var string
43     */
44    private $hostname;
45
46    /**
47     * @param string $appprefix Application prefix to use, channel will be appended.
48     * @param string $host Syslog host
49     * @param int $port Syslog port
50     * @param int $facility Syslog message facility
51     * @param int|string|Level $level The minimum logging level at which this
52     *   handler will be triggered
53     * @param bool $bubble Whether the messages that are handled can bubble up
54     *   the stack or not
55     */
56    public function __construct(
57        $appprefix,
58        $host,
59        $port = 514,
60        $facility = LOG_USER,
61        $level = Level::Debug,
62        $bubble = true
63    ) {
64        parent::__construct( $host, $port, $facility, $level, $bubble );
65        $this->appprefix = $appprefix;
66        $this->hostname = php_uname( 'n' );
67    }
68
69    protected function syslogHeader( int $severity, string $app ): string {
70        $pri = $severity + $this->facility;
71
72        // Goofy date format courtesy of RFC 3164 :(
73        // RFC 3164 actually specifies that the day of month should be space
74        // padded rather than unpadded but this seems to work with rsyslog and
75        // Logstash.
76        $timestamp = date( 'M j H:i:s' );
77
78        return "<{$pri}>{$timestamp} {$this->hostname} {$app}: ";
79    }
80
81    /**
82     * @param string|string[] $message
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( LogRecord $record ): void {
93        $lines = $this->splitMessageIntoLines( $record->formatted );
94        $header = $this->syslogHeader(
95            $this->toSyslogPriority( $record->level ),
96            $this->appprefix . $record->channel );
97
98        foreach ( $lines as $line ) {
99            $this->socket->write( $line, $header );
100        }
101    }
102}