Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / debug / logger / monolog / SyslogHandler.php
blob104ee5808f487917120c4f6b5dd044d4e2756e0a
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.
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
18 * @file
21 namespace MediaWiki\Logger\Monolog;
23 use Monolog\Handler\SyslogUdpHandler;
24 use Monolog\Logger;
26 /**
27 * Log handler that supports sending log events to a syslog server using RFC
28 * 3164 formatted UDP packets.
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.
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:
40 * <PRI>DATETIME HOSTNAME PROGRAM: MESSAGE
42 * This format works as input to rsyslog and can also be processed by the
43 * default Logstash syslog input handler.
45 * @since 1.25
46 * @author Bryan Davis <bd808@wikimedia.org>
47 * @copyright © 2015 Bryan Davis and Wikimedia Foundation.
49 class SyslogHandler extends SyslogUdpHandler {
51 /**
52 * @var string $appname
54 private $appname;
56 /**
57 * @var string $hostname
59 private $hostname;
61 /**
62 * @param string $appname Application name to report to syslog
63 * @param string $host Syslog host
64 * @param int $port Syslog port
65 * @param int $facility Syslog message facility
66 * @param string $level The minimum logging level at which this handler
67 * will be triggered
68 * @param bool $bubble Whether the messages that are handled can bubble up
69 * the stack or not
71 public function __construct(
72 $appname,
73 $host,
74 $port = 514,
75 $facility = LOG_USER,
76 $level = Logger::DEBUG,
77 $bubble = true
78 ) {
79 parent::__construct( $host, $port, $facility, $level, $bubble );
80 $this->appname = $appname;
81 $this->hostname = php_uname( 'n' );
84 protected function makeCommonSyslogHeader( $severity ) {
85 $pri = $severity + $this->facility;
87 // Goofy date format courtesy of RFC 3164 :(
88 // RFC 3164 actually specifies that the day of month should be space
89 // padded rather than unpadded but this seems to work with rsyslog and
90 // Logstash.
91 $timestamp = date( 'M j H:i:s' );
93 return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: ";