Merge "MimeAnalyzer: Add testcases for mp3 detection"
[lhc/web/wiklou.git] / includes / debug / logger / monolog / SyslogHandler.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Logger\Monolog;
22
23 use Monolog\Handler\SyslogUdpHandler;
24 use Monolog\Logger;
25
26 /**
27 * Log handler that supports sending log events to a syslog server using RFC
28 * 3164 formatted UDP packets.
29 *
30 * Monolog's SyslogUdpHandler creates a partial RFC 5424 header (PRI and
31 * VERSION) and relies on the associated formatter to complete the header and
32 * message payload. This makes using it with a fixed format formatter like
33 * \Monolog\Formatter\LogstashFormatter impossible. Additionally, the
34 * direct syslog input for Logstash only handles RFC 3164 syslog packets.
35 *
36 * This Handler should work with any Formatter. The formatted message will be
37 * prepended with an RFC 3164 message header and a partial message body. The
38 * resulting packet will looks something like:
39 *
40 * <PRI>DATETIME HOSTNAME PROGRAM: MESSAGE
41 *
42 * This format works as input to rsyslog and can also be processed by the
43 * default Logstash syslog input handler.
44 *
45 * @since 1.25
46 * @copyright © 2015 Wikimedia Foundation and contributors
47 */
48 class SyslogHandler extends SyslogUdpHandler {
49
50 /**
51 * @var string $appname
52 */
53 private $appname;
54
55 /**
56 * @var string $hostname
57 */
58 private $hostname;
59
60 /**
61 * @param string $appname Application name to report to syslog
62 * @param string $host Syslog host
63 * @param int $port Syslog port
64 * @param int $facility Syslog message facility
65 * @param string $level The minimum logging level at which this handler
66 * will be triggered
67 * @param bool $bubble Whether the messages that are handled can bubble up
68 * the stack or not
69 */
70 public function __construct(
71 $appname,
72 $host,
73 $port = 514,
74 $facility = LOG_USER,
75 $level = Logger::DEBUG,
76 $bubble = true
77 ) {
78 parent::__construct( $host, $port, $facility, $level, $bubble );
79 $this->appname = $appname;
80 $this->hostname = php_uname( 'n' );
81 }
82
83 protected function makeCommonSyslogHeader( $severity ) {
84 $pri = $severity + $this->facility;
85
86 // Goofy date format courtesy of RFC 3164 :(
87 // RFC 3164 actually specifies that the day of month should be space
88 // padded rather than unpadded but this seems to work with rsyslog and
89 // Logstash.
90 $timestamp = date( 'M j H:i:s' );
91
92 return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: ";
93 }
94 }